Skip to content

Commit deac222

Browse files
committed
fix global.
1 parent 226ed84 commit deac222

File tree

2 files changed

+21
-15
lines changed

2 files changed

+21
-15
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ clean:
88
rm -f *.dll
99
else
1010
libv8wrap.so : v8wrap.cc v8.go
11-
g++ -fPIC -shared -o libv8wrap.so -I. v8wrap.cc -lv8
12-
go build -x .
11+
g++ -fPIC -shared -o libv8wrap.so -I. v8wrap.cc -lv8_base -lv8_snapshot
12+
go build .
1313

1414
clean:
1515
rm -f *.so

v8wrap.cc

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77

88
extern "C" {
99

10-
static v8wrap_callback _go_callback = NULL;
11-
static v8::Handle<v8::ObjectTemplate> global;
10+
static volatile v8wrap_callback __go_callback = NULL;
1211

1312
static std::string
1413
to_json(v8::Handle<v8::Value> value) {
@@ -44,41 +43,48 @@ _go_call(const v8::Arguments& args) {
4443
uint32_t id = args[0]->ToUint32()->Value();
4544
v8::String::Utf8Value name(args[1]);
4645
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);
5049
if (retv != NULL) {
51-
ret = from_json(retv);
50+
v8::Handle<v8::Value> ret = from_json(retv);
5251
free(retv);
52+
return ret;
5353
}
54-
return ret;
54+
return v8::Undefined();
5555
}
5656

5757
class V8Context {
5858
public:
5959
V8Context() {
6060
v8::Locker v8Locker;
6161
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_);
6666
context_ = v8::Persistent<v8::Context>::New(context);
6767
};
6868

69-
virtual ~V8Context() { context_.Dispose(); };
69+
virtual ~V8Context() {
70+
context_.Dispose();
71+
global_.Dispose();
72+
};
7073
v8::Handle<v8::Context> context() { return context_; };
7174
std::string err() const { return err_; };
7275
void err(const std::string err) { this->err_ = err; }
7376

7477
private:
78+
v8::Persistent<v8::ObjectTemplate> global_;
7579
v8::Persistent<v8::Context> context_;
80+
v8::HandleScope handle_scope_;
7681
std::string err_;
7782
};
7883

7984
void
8085
v8_init(void *p) {
81-
_go_callback = (v8wrap_callback) p;
86+
v8::HandleScope scope;
87+
__go_callback = (v8wrap_callback) p;
8288
}
8389

8490
void*

0 commit comments

Comments
 (0)