PageRenderTime 21ms CodeModel.GetById 9ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/c_src/erlv8_script.cc

http://github.com/beamjs/erlv8
C++ | 76 lines | 61 code | 15 blank | 0 comment | 5 complexity | 6ffccf53c5a6079d011a7424db852d2c MD5 | raw file
 1#include "erlv8.hh"
 2
 3TickHandler(ScriptTickHandler) {
 4  ErlNifEnv *ref_env = enif_alloc_env();
 5  ERL_NIF_TERM script_ref = enif_make_copy(ref_env, tick_ref);
 6  unsigned len;
 7  enif_get_list_length(vm->env, array[2], &len);
 8  char * buf = (char *) malloc(len + 1);
 9  enif_get_string(vm->env,array[2],buf,len + 1, ERL_NIF_LATIN1);
10  
11  ctx_res_t *res;
12  if (enif_get_resource(vm->env,array[1],ctx_resource,(void **)(&res))) {
13    TRACE("(%p) script - 1\n", vm->isolate);
14    v8::Isolate::Scope iscope(vm->isolate);
15    TRACE("(%p) script - 2\n", vm->isolate);
16    v8::Context::Scope context_scope(res->ctx);
17    TRACE("(%p) script - 3\n", vm->isolate);
18    v8::HandleScope handle_scope;
19    TRACE("(%p) script - 4\n", vm->isolate);
20    
21
22    v8::TryCatch try_catch;
23
24    TRACE("(%p) script - 4a\n", vm->isolate);
25
26   
27    v8::ScriptOrigin * origin = new v8::ScriptOrigin(term_to_js(res->ctx,vm->isolate,vm->env,array[3])->ToString(),
28						     term_to_js(res->ctx,vm->isolate,vm->env,array[4])->ToInteger(),
29						     term_to_js(res->ctx,vm->isolate,vm->env,array[5])->ToInteger());
30    
31    TRACE("(%p) script - 5\n", vm->isolate);
32
33    v8::Handle<v8::String> script = v8::String::New(buf, len);
34    v8::Handle<v8::Script> compiled = v8::Script::Compile(script,origin);
35    
36    TRACE("(%p) script - 6\n", vm->isolate);
37
38
39    delete origin;
40    
41    if (compiled.IsEmpty()) {
42      SEND(vm->server,
43	   enif_make_tuple3(env,
44			    enif_make_atom(env,"result"),
45			    enif_make_copy(env, script_ref),
46			    enif_make_tuple2(env,
47					     enif_make_atom(env,"throw"),
48					     js_to_term(res->ctx,vm->isolate,env,try_catch.Exception()))));
49    } else {
50      TRACE("(%p) script - 7\n", vm->isolate);
51
52      v8::Handle<v8::Value> value = compiled->Run();
53      TRACE("(%p) script - 8\n", vm->isolate);
54    
55      if (value.IsEmpty()) {
56	SEND(vm->server,enif_make_tuple3(env,
57					 enif_make_atom(env,"result"),
58					 enif_make_copy(env, script_ref),
59					 enif_make_tuple2(env,
60							  enif_make_atom(env,"throw"),
61							  js_to_term(res->ctx,vm->isolate,env,try_catch.Exception()))));
62      } else {
63	SEND(vm->server,enif_make_tuple3(env,
64					 enif_make_atom(env,"result"),
65					 enif_make_copy(env, script_ref),
66					 enif_make_tuple2(env, enif_make_atom(env,"ok"),
67							  js_to_term(res->ctx,vm->isolate,env,value))));
68      }
69    }
70  }
71  enif_free_env(ref_env);
72  free(buf);
73  TickHandlerResolution result;
74  result.type = DONE;
75  return result;
76}