PageRenderTime 45ms CodeModel.GetById 35ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/c_src/erlv8_extern.cc

http://github.com/beamjs/erlv8
C++ | 89 lines | 74 code | 15 blank | 0 comment | 18 complexity | f3b0115473ce79e7e8523af03927aa28 MD5 | raw file
 1#include "erlv8.hh"
 2
 3v8::Handle<v8::Object> extern_name_to_proto(VM * vm, char *name) {
 4  v8::Handle<v8::Object> proto;
 5
 6  if (!strcmp(name,"num")) {
 7	proto = vm->external_proto_num;
 8  } else if (!strcmp(name,"atom")) {
 9	proto = vm->external_proto_atom;
10  } else if (!strcmp(name,"bin")) {
11	proto = vm->external_proto_bin;
12  } else if (!strcmp(name,"ref")) {
13	proto = vm->external_proto_ref;
14  } else if (!strcmp(name,"fun")) {
15	proto = vm->external_proto_fun;
16  } else if (!strcmp(name,"port")) {
17	proto = vm->external_proto_port;
18  } else if (!strcmp(name,"pid")) {
19	proto = vm->external_proto_pid;
20  } else if (!strcmp(name,"tuple")) {
21	proto = vm->external_proto_tuple;
22  } else if (!strcmp(name,"list")) {
23	proto = vm->external_proto_list;
24  } else {
25	proto = v8::Object::New();
26  }
27
28  return proto;
29}
30
31TickHandler(ExternProtoTickHandler) {
32  ErlNifEnv *ref_env = enif_alloc_env();
33  ERL_NIF_TERM extern_proto_ref = enif_make_copy(ref_env, tick_ref);
34  char name[MAX_ATOM_LEN];
35  unsigned len;
36
37  enif_get_atom_length(vm->env, array[1], &len, ERL_NIF_LATIN1);
38  enif_get_atom(vm->env,array[1],(char *)&name,len + 1, ERL_NIF_LATIN1);
39  
40  v8::Handle<v8::Object> proto = extern_name_to_proto(vm, name);
41
42  SEND(vm->server,
43	   enif_make_tuple3(env,
44			    enif_make_atom(env,"result"),
45			    enif_make_copy(env,extern_proto_ref),
46			    js_to_term(vm->context, vm->isolate, env, proto)));
47
48  enif_free_env(ref_env);
49  TickHandlerResolution result;
50  result.type = DONE;
51  return result;
52}
53
54TickHandler(ExternalizeTickHandler) {
55  ErlNifEnv *ref_env = enif_alloc_env();
56  ERL_NIF_TERM extern_ref = enif_make_copy(ref_env, tick_ref);
57  char name[MAX_ATOM_LEN];
58  unsigned len;
59
60  enif_get_atom_length(vm->env, array[1], &len, ERL_NIF_LATIN1);
61  enif_get_atom(vm->env,array[1],(char *)&name,len + 1, ERL_NIF_LATIN1);
62  
63  v8::Handle<v8::Object> proto = extern_name_to_proto(vm, name);
64  v8::Handle<v8::Object> obj = externalize_term(vm->extern_map, proto, array[2]);
65  
66  ERL_NIF_TERM resource_term;
67	  
68  val_res_t *ptr;
69  ptr = (val_res_t *)enif_alloc_resource(val_resource, sizeof(val_res_t));
70  ptr->val = v8::Persistent<v8::Object>::New(v8::Handle<v8::Object>::Cast(obj));
71  ptr->ctx = v8::Persistent<v8::Context>::New(v8::Context::GetCurrent());
72  resource_term = enif_make_resource(vm->env, ptr);
73  enif_release_resource(ptr);
74
75  SEND(vm->server,
76	   enif_make_tuple3(env,
77						enif_make_atom(env,"result"),
78						enif_make_copy(env,extern_ref),
79						enif_make_tuple3(env,
80										 enif_make_atom(env, "erlv8_object"),
81										 enif_make_copy(env, resource_term),
82										 enif_make_pid(env, vm->server)
83										 )));
84
85  enif_free_env(ref_env);
86  TickHandlerResolution result;
87  result.type = DONE;
88  return result;
89}