Commits

Anonymous committed fb82f96

run multiple scripts in a row yeah

  • Participants
  • Parent commits 9977af2

Comments (0)

Files changed (2)

 	    {error, timeout}
     end.
 
-test() ->
-    io:format("load v8: ~w~n", [load()]),
-    Context = context(),
-    case script(Context, <<"var f = function() { return true; }; g();">>) of
+print_script_result(Context, Script) ->
+    case script(Context, Script) of
 	{ok, Result} ->
 	    io:format("script value: ~w~n", [Result]);
 	{error, Error} ->
 	    io:format("script error: ~s~n", [Error])
-    end,
+    end.
+
+test() ->
+    io:format("load v8: ~w~n", [load()]),
+    Context = context(),
+    print_script_result(Context, <<"true;">>),
+    print_script_result(Context, <<"var f = function() { return true; }; f();">>),
+    print_script_result(Context, <<"var f = function() { return true; }; g();">>),
     destroy(Context),
     ok.
     

File src/driver.cc

 static void run_script(void *);
 static void async_free(void *);
 
+static char OK[] = "ok";
 static char ERROR[] = "error";
 static char COMPILER_ERROR[] = "compilation failed";
 static char DRIVER_NAME[] = "v8erl";
   void init(ErlDrvPort);
   void dispose();
   ErlDrvData castOut();
-  unsigned int *key();
   static PortContext *castIn(void *);
 };
 
   return reinterpret_cast<ErlDrvData>(this); 
 };
 
-unsigned int *PortContext::key() {
-  return reinterpret_cast<unsigned int *>(&port);
-};
-
 PortContext *PortContext::castIn(void *data) {
   return reinterpret_cast<PortContext *>(data);
 };
 };
 
 void AsyncData::setResult(Handle<Value> result) {
-  tlen = terms_length(result);
+  tlen = terms_length(result) + 4;
   terms = reinterpret_cast<ErlDrvTermData *>(driver_alloc(sizeof(ErlDrvTermData) * tlen));
-  value_to_terms(result, terms);
+  terms[0] = ERL_DRV_ATOM;
+  terms[1] = driver_mk_atom(OK);
+  value_to_terms(result, terms + 2);
+  terms[tlen - 2] = ERL_DRV_TUPLE;
+  terms[tlen - 1] = 2;
 };
 
 void AsyncData::send() {
 }
 
 static void outputv(ErlDrvData edata, ErlIOVec *ev) {
+  printf("outputv\r\n");
   ErlDrvBinary* input = ev->binv[1];
 
   PortContext *data = PortContext::castIn(edata);
   AsyncData *adata = AsyncData::castIn(driver_alloc(sizeof(AsyncData)));
   adata->init(data->port, data->context, input->orig_bytes, input->orig_size);
-  driver_async(data->port, data->key(), run_script, adata, async_free);
+  driver_async(data->port, NULL, run_script, adata, async_free);
 }
 
 static void ready_async(ErlDrvData edata, ErlDrvThreadData tdata) {
+  printf("ready_async\r\n");
   AsyncData *adata = AsyncData::castIn(tdata);
   adata->send();
   adata->dispose();
 }
 
 static void run_script(void *tdata) {
+  printf("run_script\r\n");
   AsyncData *adata = AsyncData::castIn(tdata);
  
   Context::Scope context_scope(adata->context);