github.com/jbking/gohan@v0.0.0-20151217002006-b41ccf1c2a96/extension/v8/gohan_core.go (about) 1 // Copyright (C) 2015 NTT Innovation Institute, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 12 // implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 16 // +build v8 17 // Copyright (C) 2015 NTT Innovation Institute, Inc. 18 // 19 // Licensed under the Apache License, Version 2.0 (the "License"); 20 // you may not use this file except in compliance with the License. 21 // You may obtain a copy of the License at 22 // 23 // http://www.apache.org/licenses/LICENSE-2.0 24 // 25 // Unless required by applicable law or agreed to in writing, software 26 // distributed under the License is distributed on an "AS IS" BASIS, 27 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 28 // implied. 29 // See the License for the specific language governing permissions and 30 // limitations under the License. 31 32 package v8 33 34 const gohanBuildin = ` 35 var v8rpc = { 36 callbacks: [], 37 objects: [], 38 call_count: 0, 39 init: function(){ 40 var self = this; 41 $recv(function(messageString){ 42 var message = JSON.parse(messageString) 43 var context = message.context 44 if(context.type === "reply"){ 45 var callback = self.callbacks[context.id]; 46 callback(message.reply); 47 delete self.callbacks[context.id]; 48 }else if(context.type == "cast"){ 49 $print("cast from go: " + message.method); 50 var object = self.objects[message.object]; 51 if(object){ 52 object[message.method].apply(object, message.args) 53 } 54 }else if(context.type == "call"){ 55 $print("call from go: " + message.method); 56 var object = self.objects[message.object]; 57 if(object){ 58 reply = object[message.method].apply(object, message.args) 59 } 60 context.type = "reply"; 61 message.reply = reply; 62 self.send(message) 63 } 64 }); 65 }, 66 newCallbackID: function(){ 67 return "" + this.call_count++; 68 }, 69 send: function(object){ 70 var messageString = JSON.stringify(object); 71 $send(messageString); 72 }, 73 cast: function(object, method, args){ 74 this.send({"context":{"type": "cast"}, "object": object, "args": args, "method": method}); 75 }, 76 call: function(object, method, args, callback){ 77 var callbackID = this.newCallbackID(); 78 this.callbacks[callbackID] = callback; 79 var context = {"id": callbackID, "type": "call"} 80 this.send({"context": context, "args": args, "object": object, "method": method}); 81 }, 82 register_object: function(objectID, object){ 83 this.objects[objectID] = object; 84 } 85 } 86 87 v8rpc.init(); 88 89 var gohan_handler = {} 90 91 function gohan_register_handler(event_type, func){ 92 if(gohan_handler[event_type] == undefined){ 93 gohan_handler[event_type] = []; 94 } 95 gohan_handler[event_type].push(func) 96 } 97 98 var gohan_handler = { 99 handle_event: function(context, event_type){ 100 if(gohan_handler[event_type] == undefined){ 101 return 102 } 103 for (var i = 0; i < gohan_handler[event_type].length; ++i) { 104 gohan_handler[event_type][i](context); 105 } 106 return context 107 } 108 }; 109 110 v8rpc.register_object("gohan_handler", gohan_handler); 111 112 ` 113 114 func init() { 115 gohanInit := func(env *Environment) { 116 env.rpc.Load("<Gohan built-ins>", gohanBuildin) 117 } 118 RegisterInit(gohanInit) 119 }