Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge branch 'context_scope_fixups'

Conflicts:
	c_src/erlv8_term.cc
  • Loading branch information...
commit 8aeb1c84633e8e93975808fe1c375f9b41e27b1b 2 parents 349be72 + 8547f3d
@yrashk yrashk authored
View
37 c_src/erlv8.cc
@@ -1,6 +1,6 @@
#include "erlv8.hh"
-typedef TickHandlerResolution (*TickHandler)(VM *, char *, ERL_NIF_TERM, ERL_NIF_TERM, ERL_NIF_TERM, int, const ERL_NIF_TERM*, v8::Handle<v8::Value>&);
+typedef TickHandlerResolution (*TickHandler)(VM *, char *, ERL_NIF_TERM, ERL_NIF_TERM, ERL_NIF_TERM, int, const ERL_NIF_TERM*);
struct ErlV8TickHandler {
const char * name;
@@ -106,13 +106,13 @@ VM::~VM() {
};
void VM::run() {
+ v8::Locker locker;
+ v8::HandleScope handle_scope; // the very top level handle scope
ticker(0);
};
v8::Handle<v8::Value> VM::ticker(ERL_NIF_TERM ref0) {
- v8::Locker locker;
- v8::Context::Scope context_scope(context);
-
+ LHCS(context);
char name[MAX_ATOM_LEN];
unsigned len;
@@ -131,10 +131,7 @@ v8::Handle<v8::Value> VM::ticker(ERL_NIF_TERM ref0) {
zmq_msg_t msg;
Tick tick_s;
ERL_NIF_TERM tick, tick_ref;
-
while (1) {
- v8::HandleScope handle_scope;
-
{
v8::Unlocker unlocker;
zmq_msg_init (&msg);
@@ -159,14 +156,15 @@ v8::Handle<v8::Value> VM::ticker(ERL_NIF_TERM ref0) {
enif_get_atom(env,array[0],(char *)&name,len + 1, ERL_NIF_LATIN1);
// lookup the matrix
- v8::Handle<v8::Value> result;
unsigned int i = 0;
bool stop_flag = false;
while (!stop_flag) {
if ((!tick_handlers[i].name) ||
(!strcmp(name,tick_handlers[i].name))) { // handler has been located
- switch (tick_handlers[i].handler(this, name, tick, tick_ref, ref, arity, array, result)) {
+ TickHandlerResolution resolution = (tick_handlers[i].handler(this, name, tick, tick_ref, ref, arity, array));
+
+ switch (resolution.type) {
case DONE:
stop_flag = true;
break;
@@ -190,8 +188,7 @@ v8::Handle<v8::Value> VM::ticker(ERL_NIF_TERM ref0) {
zmq_msg_close(&tick_msg);
}
-
- return result;
+ return handle_scope.Close(resolution.value);
break;
}
}
@@ -329,7 +326,7 @@ static ERL_NIF_TERM global(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
if (enif_get_resource(env,argv[0],ctx_resource,(void **)(&res))) {
LHCS(res->ctx);
v8::Handle<v8::Object> global = res->ctx->Global();
- return js_to_term(env,global);
+ return js_to_term(res->ctx,env,global);
} else {
return enif_make_badarg(env);
};
@@ -383,7 +380,7 @@ v8::Handle<v8::Value> WrapFun(const v8::Arguments &arguments) {
// prepare arguments
ERL_NIF_TERM *arr = (ERL_NIF_TERM *) malloc(sizeof(ERL_NIF_TERM) * arguments.Length());
for (int i=0;i<arguments.Length();i++) {
- arr[i] = js_to_term(vm->env,arguments[i]);
+ arr[i] = js_to_term(vm->context,vm->env,arguments[i]);
}
ERL_NIF_TERM arglist = enif_make_list_from_array(vm->env,arr,arguments.Length());
free(arr);
@@ -394,14 +391,14 @@ v8::Handle<v8::Value> WrapFun(const v8::Arguments &arguments) {
enif_make_tuple7(env,
enif_make_atom(env,"erlv8_fun_invocation"),
enif_make_atom(env,arguments.IsConstructCall() ? "true" : "false"),
- js_to_term(env, arguments.Holder()),
- js_to_term(env, arguments.This()),
+ js_to_term(vm->context, env, arguments.Holder()),
+ js_to_term(vm->context, env, arguments.This()),
enif_make_copy(env, ref),
enif_make_pid(env, vm->server),
enif_make_copy(env, external_to_term(v8::Context::GetCurrent()->Global()->GetHiddenValue(v8::String::New("__erlv8__ctx__"))))
),
enif_make_copy(env,arglist)));
- return vm->ticker(ref);
+ return handle_scope.Close(vm->ticker(ref));
};
@@ -430,6 +427,11 @@ int load(ErlNifEnv *env, void** priv_data, ERL_NIF_TERM load_info)
ctx_resource = enif_open_resource_type(env, NULL, "erlv8_ctx_resource", ctx_resource_destroy, (ErlNifResourceFlags) (ERL_NIF_RT_CREATE | ERL_NIF_RT_TAKEOVER), NULL);
v8::V8::Initialize();
+ int preemption = 100; // default value
+ enif_get_int(env, load_info, &preemption);
+ v8::Locker locker;
+ v8::Locker::StartPreemption(preemption);
+
v8::HandleScope handle_scope;
global_template = v8::Persistent<v8::ObjectTemplate>::New(v8::ObjectTemplate::New());
@@ -440,9 +442,6 @@ int load(ErlNifEnv *env, void** priv_data, ERL_NIF_TERM load_info)
string__erlv8__ = v8::Persistent<v8::String>::New(v8::String::New("__erlv8__"));
- int preemption = 100; // default value
- enif_get_int(env, load_info, &preemption);
- v8::Locker::StartPreemption(preemption);
return 0;
};
View
13 c_src/erlv8.hh
@@ -119,9 +119,14 @@ public:
};
-enum TickHandlerResolution { DONE, RETURN, NEXT };
+enum TickHandlerResolutionType { DONE, RETURN, NEXT };
-#define TickHandler(name) extern TickHandlerResolution name(VM * vm, char * tick_name, ERL_NIF_TERM tick, ERL_NIF_TERM tick_ref, ERL_NIF_TERM ref, int arity, const ERL_NIF_TERM * array, v8::Handle<v8::Value>& result)
+typedef struct {
+ v8::Handle<v8::Value> value;
+ TickHandlerResolutionType type;
+} TickHandlerResolution;
+
+#define TickHandler(name) extern TickHandlerResolution name(VM * vm, char * tick_name, ERL_NIF_TERM tick, ERL_NIF_TERM tick_ref, ERL_NIF_TERM ref, int arity, const ERL_NIF_TERM * array)
TickHandler(StopTickHandler);
TickHandler(ResultTickHandler);
@@ -174,8 +179,8 @@ public:
#define __ERLV8__(O) v8::Local<v8::External>::Cast(O->GetHiddenValue(string__erlv8__))->Value()
-v8::Handle<v8::Value> term_to_js(ErlNifEnv *env, ERL_NIF_TERM term);
-ERL_NIF_TERM js_to_term(ErlNifEnv *env, v8::Handle<v8::Value> val);
+v8::Handle<v8::Value> term_to_js(v8::Handle<v8::Context> ctx, ErlNifEnv *env, ERL_NIF_TERM term);
+ERL_NIF_TERM js_to_term(v8::Handle<v8::Context> ctx, ErlNifEnv *env, v8::Handle<v8::Value> val);
ERL_NIF_TERM external_to_term(v8::Handle<v8::Value> val);
v8::Handle<v8::Value> term_to_external(ERL_NIF_TERM term);
v8::PropertyAttribute term_to_property_attribute(ErlNifEnv * env, ERL_NIF_TERM term);
View
18 c_src/erlv8_call.cc
@@ -17,12 +17,12 @@ TickHandler(CallTickHandler) {
args = new v8::Local<v8::Value>[alen];
int i = 0;
while (enif_get_list_cell(vm->env, current, &head, &tail)) {
- args[i] = v8::Local<v8::Value>::New(term_to_js(vm->env,head));
+ args[i] = v8::Local<v8::Value>::New(term_to_js(fun_res->ctx,vm->env,head));
i++; current = tail;
}
v8::Local<v8::Object> recv;
if (arity == 4) { // this is specified
- recv = term_to_js(vm->env, array[3])->ToObject();
+ recv = term_to_js(fun_res->ctx,vm->env, array[3])->ToObject();
} else {
recv = fun_res->ctx->Global();
}
@@ -40,13 +40,13 @@ TickHandler(CallTickHandler) {
enif_make_atom(env,"throw"),
enif_make_tuple2(env,
enif_make_atom(env,"error"),
- js_to_term(env,try_catch.Exception())))));
+ js_to_term(fun_res->ctx,env,try_catch.Exception())))));
} else {
SEND(vm->server,
enif_make_tuple3(env,
enif_make_atom(env,"result"),
enif_make_copy(env,call_ref),
- js_to_term(env,call_result)));
+ js_to_term(fun_res->ctx,env,call_result)));
}
} else { // native Erlang function
v8::Local<v8::Array> array = v8::Array::New(alen);
@@ -62,7 +62,9 @@ TickHandler(CallTickHandler) {
args = NULL;
}
enif_free_env(ref_env);
- return DONE;
+ TickHandlerResolution result;
+ result.type = DONE;
+ return result;
}
void ErlangFun(VM * vm, ERL_NIF_TERM term, ERL_NIF_TERM ref, v8::Handle<v8::Object> recv, v8::Handle<v8::Array> array) {
@@ -71,7 +73,7 @@ void ErlangFun(VM * vm, ERL_NIF_TERM term, ERL_NIF_TERM ref, v8::Handle<v8::Obje
// prepare arguments
ERL_NIF_TERM *arr = (ERL_NIF_TERM *) malloc(sizeof(ERL_NIF_TERM) * array->Length());
for (unsigned int i=0;i<array->Length();i++) {
- arr[i] = js_to_term(vm->env,array->Get(v8::Integer::NewFromUnsigned(i)));
+ arr[i] = js_to_term(vm->context,vm->env,array->Get(v8::Integer::NewFromUnsigned(i)));
}
ERL_NIF_TERM arglist = enif_make_list_from_array(vm->env,arr,array->Length());
free(arr);
@@ -82,8 +84,8 @@ void ErlangFun(VM * vm, ERL_NIF_TERM term, ERL_NIF_TERM ref, v8::Handle<v8::Obje
enif_make_tuple7(env,
enif_make_atom(env,"erlv8_fun_invocation"),
enif_make_atom(env, "false"),
- js_to_term(env, recv), // FIXME: not quite sure it's right
- js_to_term(env, recv),
+ js_to_term(vm->context, env, recv), // FIXME: not quite sure it's right
+ js_to_term(vm->context, env, recv),
enif_make_copy(env, ref),
enif_make_pid(env, vm->server),
enif_make_copy(env, external_to_term(v8::Context::GetCurrent()->Global()->GetHiddenValue(v8::String::New("__erlv8__ctx__"))))
View
6 c_src/erlv8_delete.cc
@@ -4,7 +4,7 @@ TickHandler(DeleteTickHandler) {
val_res_t *obj_res;
if (enif_get_resource(vm->env,array[1],val_resource,(void **)(&obj_res))) {
LHCS(obj_res->ctx);
- v8::Handle<v8::Value> key = term_to_js(vm->env,array[2]);
+ v8::Handle<v8::Value> key = term_to_js(obj_res->ctx,vm->env,array[2]);
if (key->IsString()) {
obj_res->val->ToObject()->Delete(key->ToString());
} else if (key->IsNumber()) {
@@ -17,6 +17,8 @@ TickHandler(DeleteTickHandler) {
enif_make_copy(env,tick_ref),
enif_make_atom(env, "ok")));
}
- return DONE;
+ TickHandlerResolution result;
+ result.type = DONE;
+ return result;
}
View
12 c_src/erlv8_equals.cc
@@ -13,7 +13,7 @@ TickHandler(EqualsTickHandler) {
bres = res1->val->ToObject()->Equals(res2->val->ToObject());
} else {
LHCS(vm->context);
- bres = term_to_js(vm->env,array[1])->Equals(term_to_js(vm->env,array[2]));
+ bres = term_to_js(vm->context,vm->env,array[1])->Equals(term_to_js(vm->context,vm->env,array[2]));
};
SEND(vm->server,
@@ -23,7 +23,9 @@ TickHandler(EqualsTickHandler) {
enif_make_atom(env, bres ? "true" : "false")));
enif_free_env(ref_env);
- return DONE;
+ TickHandlerResolution result;
+ result.type = DONE;
+ return result;
}
TickHandler(StrictEqualsTickHandler) {
@@ -39,7 +41,7 @@ TickHandler(StrictEqualsTickHandler) {
bres = res1->val->ToObject()->StrictEquals(res2->val->ToObject());
} else {
LHCS(vm->context);
- bres = term_to_js(vm->env,array[1])->StrictEquals(term_to_js(vm->env,array[2]));
+ bres = term_to_js(vm->context,vm->env,array[1])->StrictEquals(term_to_js(vm->context,vm->env,array[2]));
};
SEND(vm->server,
@@ -49,5 +51,7 @@ TickHandler(StrictEqualsTickHandler) {
enif_make_atom(env, bres ? "true" : "false")));
enif_free_env(ref_env);
- return DONE;
+ TickHandlerResolution result;
+ result.type = DONE;
+ return result;
}
View
10 c_src/erlv8_extern.cc
@@ -43,10 +43,12 @@ TickHandler(ExternProtoTickHandler) {
enif_make_tuple3(env,
enif_make_atom(env,"result"),
enif_make_copy(env,extern_proto_ref),
- js_to_term(env,proto)));
+ js_to_term(vm->context,env,proto)));
enif_free_env(ref_env);
- return DONE;
+ TickHandlerResolution result;
+ result.type = DONE;
+ return result;
}
TickHandler(ExternalizeTickHandler) {
@@ -81,5 +83,7 @@ TickHandler(ExternalizeTickHandler) {
)));
enif_free_env(ref_env);
- return DONE;
+ TickHandlerResolution result;
+ result.type = DONE;
+ return result;
}
View
4 c_src/erlv8_gc.cc
@@ -3,5 +3,7 @@
TickHandler(GCTickHandler) {
while (!v8::V8::IdleNotification())
;
- return DONE;
+ TickHandlerResolution result;
+ result.type = DONE;
+ return result;
}
View
28 c_src/erlv8_get.cc
@@ -4,15 +4,17 @@ TickHandler(GetTickHandler) {
val_res_t *obj_res;
if (enif_get_resource(vm->env,array[1],val_resource,(void **)(&obj_res))) {
LHCS(obj_res->ctx);
- v8::Local<v8::Value> get_result = obj_res->val->ToObject()->Get(term_to_js(vm->env,array[2]));
+ v8::Local<v8::Value> get_result = obj_res->val->ToObject()->Get(term_to_js(obj_res->ctx,vm->env,array[2]));
SEND(vm->server,
enif_make_tuple3(env,
enif_make_atom(env,"result"),
enif_make_copy(env,tick_ref),
- js_to_term(env,get_result)));
+ js_to_term(obj_res->ctx,env,get_result)));
}
- return DONE;
+ TickHandlerResolution result;
+ result.type = DONE;
+ return result;
}
TickHandler(GetProtoTickHandler) {
@@ -25,24 +27,28 @@ TickHandler(GetProtoTickHandler) {
enif_make_tuple3(env,
enif_make_atom(env,"result"),
enif_make_copy(env,tick_ref),
- js_to_term(env,get_result)));
+ js_to_term(obj_res->ctx,env,get_result)));
}
- return DONE;
+ TickHandlerResolution result;
+ result.type = DONE;
+ return result;
}
TickHandler(GetHiddenTickHandler) {
val_res_t *obj_res;
if (enif_get_resource(vm->env,array[1],val_resource,(void **)(&obj_res))) {
LHCS(obj_res->ctx);
- v8::Local<v8::Value> get_result = obj_res->val->ToObject()->GetHiddenValue(term_to_js(vm->env,array[2])->ToString());
+ v8::Local<v8::Value> get_result = obj_res->val->ToObject()->GetHiddenValue(term_to_js(obj_res->ctx,vm->env,array[2])->ToString());
SEND(vm->server,
enif_make_tuple3(env,
enif_make_atom(env,"result"),
enif_make_copy(env,tick_ref),
- js_to_term(env,get_result)));
+ js_to_term(obj_res->ctx,env,get_result)));
}
- return DONE;
+ TickHandlerResolution result;
+ result.type = DONE;
+ return result;
}
TickHandler(GetInternalTickHandler) {
@@ -74,10 +80,12 @@ TickHandler(GetInternalTickHandler) {
enif_make_tuple3(env,
enif_make_atom(env,"result"),
enif_make_copy(env,tick_ref),
- js_to_term(env,get_result)));
+ js_to_term(obj_res->ctx,env,get_result)));
}
}
}
- return DONE;
+ TickHandlerResolution result;
+ result.type = DONE;
+ return result;
}
View
16 c_src/erlv8_instantiate.cc
@@ -17,7 +17,7 @@ TickHandler(InstantiateTickHandler) {
args = new v8::Local<v8::Value>[alen];
int i = 0;
while (enif_get_list_cell(vm->env, current, &head, &tail)) {
- args[i] = v8::Local<v8::Value>::New(term_to_js(vm->env,head));
+ args[i] = v8::Local<v8::Value>::New(term_to_js(fun_res->ctx,vm->env,head));
i++; current = tail;
}
@@ -35,13 +35,13 @@ TickHandler(InstantiateTickHandler) {
enif_make_atom(env,"throw"),
enif_make_tuple2(env,
enif_make_atom(env,"error"),
- js_to_term(env,try_catch.Exception())))));
+ js_to_term(fun_res->ctx,env,try_catch.Exception())))));
} else {
SEND(vm->server,
enif_make_tuple3(env,
enif_make_atom(env,"result"),
enif_make_copy(env,inst_ref),
- js_to_term(env,inst_result)));
+ js_to_term(fun_res->ctx,env,inst_result)));
}
} else { // native Erlang function
v8::Local<v8::Array> array = v8::Array::New(alen);
@@ -61,7 +61,9 @@ TickHandler(InstantiateTickHandler) {
args = NULL;
}
enif_free_env(ref_env);
- return DONE;
+ TickHandlerResolution result;
+ result.type = DONE;
+ return result;
}
void ErlangConstFun(VM * vm, ERL_NIF_TERM term, ERL_NIF_TERM ref, v8::Handle<v8::Object> instance, v8::Handle<v8::Array> array) {
@@ -73,7 +75,7 @@ void ErlangConstFun(VM * vm, ERL_NIF_TERM term, ERL_NIF_TERM ref, v8::Handle<v8:
// prepare arguments
ERL_NIF_TERM *arr = (ERL_NIF_TERM *) malloc(sizeof(ERL_NIF_TERM) * array->Length());
for (unsigned int i=0;i<array->Length();i++) {
- arr[i] = js_to_term(vm->env,array->Get(v8::Integer::NewFromUnsigned(i)));
+ arr[i] = js_to_term(vm->context,vm->env,array->Get(v8::Integer::NewFromUnsigned(i)));
}
ERL_NIF_TERM arglist = enif_make_list_from_array(vm->env,arr,array->Length());
free(arr);
@@ -84,8 +86,8 @@ void ErlangConstFun(VM * vm, ERL_NIF_TERM term, ERL_NIF_TERM ref, v8::Handle<v8:
enif_make_tuple7(env,
enif_make_atom(env,"erlv8_fun_invocation"),
enif_make_atom(env, "true"),
- js_to_term(env, instance), // FIXME: not quite sure it's right
- js_to_term(env, instance),
+ js_to_term(vm->context,env, instance), // FIXME: not quite sure it's right
+ js_to_term(vm->context,env, instance),
enif_make_copy(env, ref),
enif_make_pid(env, vm->server),
enif_make_resource(env, ptr)
View
4 c_src/erlv8_internal.cc
@@ -16,5 +16,7 @@ TickHandler(InternalCountTickHandler) {
enif_make_int(env, ifc)));
}
enif_free_env(ref_env);
- return DONE;
+ TickHandlerResolution result;
+ result.type = DONE;
+ return result;
}
View
6 c_src/erlv8_list.cc
@@ -13,7 +13,7 @@ TickHandler(ListTickHandler) {
ERL_NIF_TERM *arr = (ERL_NIF_TERM *) malloc(sizeof(ERL_NIF_TERM) * array->Length());
for (unsigned int i=0;i<array->Length();i++) {
- arr[i] = js_to_term(vm->env,array->Get(v8::Integer::NewFromUnsigned(i)));
+ arr[i] = js_to_term(res->ctx,vm->env,array->Get(v8::Integer::NewFromUnsigned(i)));
}
ERL_NIF_TERM list = enif_make_list_from_array(vm->env,arr,array->Length());
free(arr);
@@ -26,5 +26,7 @@ TickHandler(ListTickHandler) {
}
enif_free_env(ref_env);
- return DONE;
+ TickHandlerResolution result;
+ result.type = DONE;
+ return result;
}
View
8 c_src/erlv8_proplist.cc
@@ -15,8 +15,8 @@ TickHandler(ProplistTickHandler) {
for (unsigned int i=0;i<keys->Length();i++) {
v8::Handle<v8::Value> key = keys->Get(v8::Integer::New(i));
arr[i] = enif_make_tuple2(vm->env,
- js_to_term(vm->env,v8::Handle<v8::String>::Cast(key)),
- js_to_term(vm->env,res->val->ToObject()->Get(key)));
+ js_to_term(res->ctx,vm->env,v8::Handle<v8::String>::Cast(key)),
+ js_to_term(res->ctx,vm->env,res->val->ToObject()->Get(key)));
}
ERL_NIF_TERM list = enif_make_list_from_array(vm->env,arr,keys->Length());
free(arr);
@@ -28,5 +28,7 @@ TickHandler(ProplistTickHandler) {
}
enif_free_env(ref_env);
- return DONE;
+ TickHandlerResolution result;
+ result.type = DONE;
+ return result;
}
View
27 c_src/erlv8_result.cc
@@ -1,19 +1,24 @@
#include "erlv8.hh"
TickHandler(ResultTickHandler) {
- if (((unsigned long) ref) &&
+ v8::HandleScope handle_scope;
+ TickHandlerResolution result;
+
+ if (((unsigned long) ref) &&
(enif_is_identical(array[1],ref))) { // this is our result
- result = term_to_js(vm->env,array[2]);
- return RETURN;
+ result.value = handle_scope.Close(term_to_js(vm->context,vm->env,array[2]));
+ result.type = RETURN;
+ return result;
} else {
- Tick newtick;
-
- newtick.env = enif_alloc_env();
- newtick.tick = enif_make_copy(newtick.env, tick);
- newtick.ref = enif_make_copy(newtick.env, tick_ref);
-
- vm->pop_ticks.push(newtick);
+ Tick newtick;
+
+ newtick.env = enif_alloc_env();
+ newtick.tick = enif_make_copy(newtick.env, tick);
+ newtick.ref = enif_make_copy(newtick.env, tick_ref);
+
+ vm->pop_ticks.push(newtick);
- return DONE;
+ result.type = DONE;
+ return result;
}
}
View
16 c_src/erlv8_script.cc
@@ -15,9 +15,9 @@ TickHandler(ScriptTickHandler) {
v8::TryCatch try_catch;
- v8::ScriptOrigin * origin = new v8::ScriptOrigin(term_to_js(vm->env,array[3])->ToString(),
- term_to_js(vm->env,array[4])->ToInteger(),
- term_to_js(vm->env,array[5])->ToInteger());
+ v8::ScriptOrigin * origin = new v8::ScriptOrigin(term_to_js(res->ctx,vm->env,array[3])->ToString(),
+ term_to_js(res->ctx,vm->env,array[4])->ToInteger(),
+ term_to_js(res->ctx,vm->env,array[5])->ToInteger());
v8::Handle<v8::String> script = v8::String::New(buf, len);
v8::Handle<v8::Script> compiled = v8::Script::Compile(script,origin);
@@ -30,7 +30,7 @@ TickHandler(ScriptTickHandler) {
enif_make_copy(env, script_ref),
enif_make_tuple2(env,
enif_make_atom(env,"throw"),
- js_to_term(env,try_catch.Exception()))));
+ js_to_term(res->ctx,env,try_catch.Exception()))));
} else {
v8::Handle<v8::Value> value = compiled->Run();
if (value.IsEmpty()) {
@@ -39,17 +39,19 @@ TickHandler(ScriptTickHandler) {
enif_make_copy(env, script_ref),
enif_make_tuple2(env,
enif_make_atom(env,"throw"),
- js_to_term(env,try_catch.Exception()))));
+ js_to_term(res->ctx,env,try_catch.Exception()))));
} else {
SEND(vm->server,enif_make_tuple3(env,
enif_make_atom(env,"result"),
enif_make_copy(env, script_ref),
enif_make_tuple2(env, enif_make_atom(env,"ok"),
- js_to_term(env,value))));
+ js_to_term(res->ctx,env,value))));
}
}
}
enif_free_env(ref_env);
free(buf);
- return DONE;
+ TickHandlerResolution result;
+ result.type = DONE;
+ return result;
}
View
48 c_src/erlv8_set.cc
@@ -13,7 +13,7 @@ TickHandler(SetTickHandler) {
property_attribute = term_to_property_attribute(vm->env,array[4]);
}
- obj_res->val->ToObject()->Set(term_to_js(vm->env,array[2]),term_to_js(tmp_env,value), property_attribute);
+ obj_res->val->ToObject()->Set(term_to_js(obj_res->ctx, vm->env,array[2]),term_to_js(obj_res->ctx, tmp_env,value), property_attribute);
SEND(vm->server,
enif_make_tuple3(env,
@@ -22,7 +22,9 @@ TickHandler(SetTickHandler) {
enif_make_copy(env,value)));
}
enif_free_env(tmp_env);
- return DONE;
+ TickHandlerResolution result;
+ result.type = DONE;
+ return result;
}
TickHandler(SetProtoTickHandler) {
@@ -30,7 +32,7 @@ TickHandler(SetProtoTickHandler) {
if (enif_get_resource(vm->env,array[1],val_resource,(void **)(&obj_res))) {
LHCS(obj_res->ctx);
- const char *atom_val = obj_res->val->ToObject()->SetPrototype(term_to_js(vm->env,array[2])) ? "true" : "false";
+ const char *atom_val = obj_res->val->ToObject()->SetPrototype(term_to_js(obj_res->ctx,vm->env,array[2])) ? "true" : "false";
SEND(vm->server,
enif_make_tuple3(env,
@@ -38,7 +40,9 @@ TickHandler(SetProtoTickHandler) {
enif_make_copy(env,tick_ref),
enif_make_atom(env,atom_val)));
}
- return DONE;
+ TickHandlerResolution result;
+ result.type = DONE;
+ return result;
}
TickHandler(SetHiddenTickHandler) {
@@ -46,7 +50,7 @@ TickHandler(SetHiddenTickHandler) {
if (enif_get_resource(vm->env,array[1],val_resource,(void **)(&obj_res))) {
LHCS(obj_res->ctx);
- obj_res->val->ToObject()->SetHiddenValue(term_to_js(vm->env,array[2])->ToString(),term_to_js(vm->env,array[3]));
+ obj_res->val->ToObject()->SetHiddenValue(term_to_js(obj_res->ctx,vm->env,array[2])->ToString(),term_to_js(obj_res->ctx,vm->env,array[3]));
SEND(vm->server,
enif_make_tuple3(env,
@@ -54,7 +58,9 @@ TickHandler(SetHiddenTickHandler) {
enif_make_copy(env,tick_ref),
enif_make_copy(env,array[2])));
}
- return DONE;
+ TickHandlerResolution result;
+ result.type = DONE;
+ return result;
}
TickHandler(SetInternalTickHandler) {
@@ -82,7 +88,7 @@ TickHandler(SetInternalTickHandler) {
v8::Handle<v8::Object> proto = extern_name_to_proto(vm, name);
obj_res->val->ToObject()->SetInternalField(index,term_to_external(array[3]));
} else {
- obj_res->val->ToObject()->SetInternalField(index,term_to_js(vm->env,array[3]));
+ obj_res->val->ToObject()->SetInternalField(index,term_to_js(obj_res->ctx,vm->env,array[3]));
}
SEND(vm->server,
@@ -92,7 +98,9 @@ TickHandler(SetInternalTickHandler) {
enif_make_copy(env,array[3])));
}
}
- return DONE;
+ TickHandlerResolution result;
+ result.type = DONE;
+ return result;
}
v8::Handle<v8::Value> GetterFun(v8::Local<v8::String> property,const v8::AccessorInfo &info); // fwd
@@ -115,7 +123,7 @@ TickHandler(SetAccessorTickHandler) {
LHCS(obj_res->ctx);
if (arity > 3) {
- v8::Handle<v8::Value> name = term_to_js(vm->env,array[2]);
+ v8::Handle<v8::Value> name = term_to_js(obj_res->ctx,vm->env,array[2]);
if (!name->IsString()) {
goto badarg;
}
@@ -124,7 +132,7 @@ TickHandler(SetAccessorTickHandler) {
v8::Persistent<v8::Object> data = v8::Persistent<v8::Object>::New(v8::Object::New());
data.MakeWeak(NULL,weak_accessor_data_cleaner); // so that we'll release externals when we're done
- if (term_to_js(vm->env,array[3])->IsUndefined()) {
+ if (term_to_js(obj_res->ctx,vm->env,array[3])->IsUndefined()) {
goto badarg;
} else {
data->SetHiddenValue(v8::String::New("_getter"), term_to_external(array[3]));
@@ -171,7 +179,9 @@ TickHandler(SetAccessorTickHandler) {
enif_make_copy(env,tick_ref),
enif_make_atom(env, atom_val)));
}
- return DONE;
+ TickHandlerResolution result;
+ result.type = DONE;
+ return result;
}
@@ -187,7 +197,7 @@ v8::Handle<v8::Value> GetterFun(v8::Local<v8::String> property,const v8::Accesso
// prepare arguments
ERL_NIF_TERM *arr = (ERL_NIF_TERM *) malloc(sizeof(ERL_NIF_TERM) * 1);
- arr[0] = js_to_term(vm->env, property);
+ arr[0] = js_to_term(vm->context, vm->env, property);
ERL_NIF_TERM arglist = enif_make_list_from_array(vm->env,arr,1);
free(arr);
@@ -198,14 +208,14 @@ v8::Handle<v8::Value> GetterFun(v8::Local<v8::String> property,const v8::Accesso
enif_make_tuple7(env,
enif_make_atom(env,"erlv8_fun_invocation"),
enif_make_atom(env,"false"),
- js_to_term(env, info.Holder()),
- js_to_term(env, info.This()),
+ js_to_term(vm->context, env, info.Holder()),
+ js_to_term(vm->context, env, info.This()),
enif_make_copy(env, ref),
enif_make_pid(env, vm->server),
enif_make_copy(env, external_to_term(v8::Context::GetCurrent()->Global()->GetHiddenValue(v8::String::New("__erlv8__ctx__"))))
),
enif_make_copy(env,arglist)));
- return vm->ticker(ref);
+ return handle_scope.Close(vm->ticker(ref));
}
void SetterFun(v8::Local<v8::String> property,v8::Local<v8::Value> value,const v8::AccessorInfo &info) {
@@ -219,8 +229,8 @@ void SetterFun(v8::Local<v8::String> property,v8::Local<v8::Value> value,const v
// prepare arguments
ERL_NIF_TERM *arr = (ERL_NIF_TERM *) malloc(sizeof(ERL_NIF_TERM) * 2);
- arr[0] = js_to_term(vm->env, property);
- arr[1] = js_to_term(vm->env, value);
+ arr[0] = js_to_term(vm->context, vm->env, property);
+ arr[1] = js_to_term(vm->context, vm->env, value);
ERL_NIF_TERM arglist = enif_make_list_from_array(vm->env,arr,2);
free(arr);
@@ -231,8 +241,8 @@ void SetterFun(v8::Local<v8::String> property,v8::Local<v8::Value> value,const v
enif_make_tuple7(env,
enif_make_atom(env,"erlv8_fun_invocation"),
enif_make_atom(env,"false"),
- js_to_term(env, info.Holder()),
- js_to_term(env, info.This()),
+ js_to_term(vm->context, env, info.Holder()),
+ js_to_term(vm->context, env, info.This()),
enif_make_copy(env, ref),
enif_make_pid(env, vm->server),
enif_make_copy(env, external_to_term(v8::Context::GetCurrent()->Global()->GetHiddenValue(v8::String::New("__erlv8__ctx__"))))
View
7 c_src/erlv8_stop.cc
@@ -1,6 +1,9 @@
#include "erlv8.hh"
TickHandler(StopTickHandler) {
- result = v8::Undefined();
- return RETURN;
+ v8::HandleScope handle_scope;
+ TickHandlerResolution result;
+ result.value = v8::Undefined();
+ result.type = RETURN;
+ return result;
}
View
12 c_src/erlv8_string.cc
@@ -7,9 +7,11 @@ TickHandler(ToStringTickHandler) {
enif_make_tuple3(env,
enif_make_atom(env,"result"),
enif_make_copy(env,to_string_ref),
- js_to_term(env,term_to_js(vm->env,array[1])->ToString())));
+ js_to_term(vm->context,env,term_to_js(vm->context,vm->env,array[1])->ToString())));
enif_free_env(ref_env);
- return DONE;
+ TickHandlerResolution result;
+ result.type = DONE;
+ return result;
};
TickHandler(ToDetailStringTickHandler) {
@@ -19,8 +21,10 @@ TickHandler(ToDetailStringTickHandler) {
enif_make_tuple3(env,
enif_make_atom(env,"result"),
enif_make_copy(env,to_string_ref),
- js_to_term(env,term_to_js(vm->env,array[1])->ToDetailString())));
+ js_to_term(vm->context,env,term_to_js(vm->context,vm->env,array[1])->ToDetailString())));
enif_free_env(ref_env);
- return DONE;
+ TickHandlerResolution result;
+ result.type = DONE;
+ return result;
};
View
6 c_src/erlv8_taint.cc
@@ -9,8 +9,10 @@ TickHandler(TaintTickHandler) {
enif_make_tuple3(env,
enif_make_atom(env,"result"),
enif_make_copy(env,taint_ref),
- js_to_term(env, term_to_js(vm->env, array[1]))));
+ js_to_term(vm->context,env,term_to_js(vm->context,vm->env, array[1]))));
enif_free_env(ref_env);
- return DONE;
+ TickHandlerResolution result;
+ result.type = DONE;
+ return result;
}
View
24 c_src/erlv8_term.cc
@@ -106,9 +106,8 @@ v8::Handle<v8::Object> externalize_term(map<ERL_NIF_TERM, v8::Handle<v8::Object>
}
-v8::Handle<v8::Value> term_to_js(ErlNifEnv *env, ERL_NIF_TERM term) {
- v8::Locker locker;
- v8::HandleScope handle_scope;
+v8::Handle<v8::Value> term_to_js(v8::Handle<v8::Context> ctx, ErlNifEnv *env, ERL_NIF_TERM term) {
+ LHCS(ctx);
int _int; unsigned int _uint; long _long; unsigned long _ulong; ErlNifSInt64 _int64; ErlNifUInt64 _uint64; double _double;
ErlNifBinary string_binary;
unsigned len;
@@ -177,8 +176,8 @@ v8::Handle<v8::Value> term_to_js(ErlNifEnv *env, ERL_NIF_TERM term) {
ERL_NIF_TERM *arr;
while (enif_get_list_cell(env, current, &head, &tail)) {
enif_get_tuple(env,head,&arity,(const ERL_NIF_TERM **)&arr);
- obj->Set(term_to_js(env,arr[0]),
- term_to_js(env,arr[1]));
+ obj->Set(term_to_js(ctx,env,arr[0]),
+ term_to_js(ctx,env,arr[1]));
current = tail;
}
@@ -194,7 +193,7 @@ v8::Handle<v8::Value> term_to_js(ErlNifEnv *env, ERL_NIF_TERM term) {
i = 0;
while (enif_get_list_cell(env, current, &head, &tail)) {
- arrobj->Set(v8::Integer::New(i), term_to_js(env,head));
+ arrobj->Set(v8::Integer::New(i), term_to_js(ctx,env,head));
current = tail;
i++;
}
@@ -206,8 +205,8 @@ v8::Handle<v8::Value> term_to_js(ErlNifEnv *env, ERL_NIF_TERM term) {
(enif_get_resource(env,array[1],val_resource,(void **)(&res)))){
return handle_scope.Close(res->val);
} else if ((isv8fun) && (enif_is_fun(env, array[1]))) {
- v8::Handle<v8::Function> f = v8::Handle<v8::Function>::Cast(term_to_js(env,array[1]));
- v8::Handle<v8::Object> o = v8::Handle<v8::Object>::Cast(term_to_js(env,array[2]));
+ v8::Handle<v8::Function> f = v8::Handle<v8::Function>::Cast(term_to_js(ctx,env,array[1]));
+ v8::Handle<v8::Object> o = v8::Handle<v8::Object>::Cast(term_to_js(ctx,env,array[2]));
v8::Local<v8::Array> keys = o->GetPropertyNames();
@@ -229,10 +228,10 @@ v8::Handle<v8::Value> term_to_js(ErlNifEnv *env, ERL_NIF_TERM term) {
int iserror = strcmp(name,"error")==0;
int isthrow = strcmp(name,"throw")==0;
if (iserror) {
- return v8::Exception::Error(v8::Handle<v8::String>::Cast(term_to_js(env,array[1])));
+ return handle_scope.Close(v8::Exception::Error(v8::Handle<v8::String>::Cast(term_to_js(ctx,env,array[1]))));
}
if (isthrow) {
- return v8::ThrowException(term_to_js(env, array[1]));
+ return v8::ThrowException(term_to_js(ctx,env, array[1]));
}
}
@@ -264,9 +263,8 @@ v8::Handle<v8::Value> term_to_js(ErlNifEnv *env, ERL_NIF_TERM term) {
};
-ERL_NIF_TERM js_to_term(ErlNifEnv *env, v8::Handle<v8::Value> val) {
- v8::Locker locker;
- v8::HandleScope handle_scope;
+ERL_NIF_TERM js_to_term(v8::Handle<v8::Context> ctx, ErlNifEnv *env, v8::Handle<v8::Value> val) {
+ LHCS(ctx);
if (val.IsEmpty()) {
return enif_make_atom(env,"undefined");
} else if (val->IsFunction()) { // the reason why this check is so high up here is because it is also an object, so it should be before any object.
View
5 c_src/erlv8_unknown.cc
@@ -1,7 +1,8 @@
#include "erlv8.hh"
TickHandler(UnknownTickHandler) {
-
- return DONE;
+ TickHandlerResolution result;
+ result.type = DONE;
+ return result;
}
Please sign in to comment.
Something went wrong with that request. Please try again.