|
7 | 7 |
|
8 | 8 | extern "C" { |
9 | 9 |
|
10 | | -static v8wrap_callback _go_callback = NULL; |
11 | | -static v8::Handle<v8::ObjectTemplate> global; |
| 10 | +static volatile v8wrap_callback __go_callback = NULL; |
12 | 11 |
|
13 | 12 | static std::string |
14 | 13 | to_json(v8::Handle<v8::Value> value) { |
@@ -44,41 +43,48 @@ _go_call(const v8::Arguments& args) { |
44 | 43 | uint32_t id = args[0]->ToUint32()->Value(); |
45 | 44 | v8::String::Utf8Value name(args[1]); |
46 | 45 | v8::String::Utf8Value argv(args[2]); |
47 | | - v8::HandleScope scope; |
48 | | - v8::Handle<v8::Value> ret = v8::Undefined(); |
49 | | - char* retv = _go_callback(id, *name, *argv); |
| 46 | + v8::TryCatch try_catch; |
| 47 | + char* retv; |
| 48 | + retv = __go_callback(id, *name, *argv); |
50 | 49 | if (retv != NULL) { |
51 | | - ret = from_json(retv); |
| 50 | + v8::Handle<v8::Value> ret = from_json(retv); |
52 | 51 | free(retv); |
| 52 | + return ret; |
53 | 53 | } |
54 | | - return ret; |
| 54 | + return v8::Undefined(); |
55 | 55 | } |
56 | 56 |
|
57 | 57 | class V8Context { |
58 | 58 | public: |
59 | 59 | V8Context() { |
60 | 60 | v8::Locker v8Locker; |
61 | 61 | v8::HandleScope scope; |
62 | | - global = v8::ObjectTemplate::New(); |
63 | | - global->Set(v8::String::New("_go_call"), |
64 | | - v8::FunctionTemplate::New(_go_call)); |
65 | | - v8::Handle<v8::Context> context = v8::Context::New(NULL, global); |
| 62 | + global_ = v8::Persistent<v8::ObjectTemplate>::New(v8::ObjectTemplate::New()); |
| 63 | + global_->Set(v8::String::New("_go_call"), |
| 64 | + v8::FunctionTemplate::New(_go_call)); |
| 65 | + v8::Handle<v8::Context> context = v8::Context::New(NULL, global_); |
66 | 66 | context_ = v8::Persistent<v8::Context>::New(context); |
67 | 67 | }; |
68 | 68 |
|
69 | | - virtual ~V8Context() { context_.Dispose(); }; |
| 69 | + virtual ~V8Context() { |
| 70 | + context_.Dispose(); |
| 71 | + global_.Dispose(); |
| 72 | + }; |
70 | 73 | v8::Handle<v8::Context> context() { return context_; }; |
71 | 74 | std::string err() const { return err_; }; |
72 | 75 | void err(const std::string err) { this->err_ = err; } |
73 | 76 |
|
74 | 77 | private: |
| 78 | + v8::Persistent<v8::ObjectTemplate> global_; |
75 | 79 | v8::Persistent<v8::Context> context_; |
| 80 | + v8::HandleScope handle_scope_; |
76 | 81 | std::string err_; |
77 | 82 | }; |
78 | 83 |
|
79 | 84 | void |
80 | 85 | v8_init(void *p) { |
81 | | - _go_callback = (v8wrap_callback) p; |
| 86 | + v8::HandleScope scope; |
| 87 | + __go_callback = (v8wrap_callback) p; |
82 | 88 | } |
83 | 89 |
|
84 | 90 | void* |
|
0 commit comments