Commits

jer...@jeremylatt.com  committed d4fff9c

more tests. send data back to caller (instead of port owner)

  • Participants
  • Parent commits 56fdeca

Comments (0)

Files changed (3)

     erlang:port_command(Context, term_to_binary({Key, Script})),
     Key.
 
-
 script_result(Context, Script) ->
-    io:format("~s~n", [Script]),
+    io:format("[v8] ~s~n", [Script]),
     Key = execute(Context, Script),
     receive
 	{Key, Response} ->
 
 test() ->
     try 
+	%% load v8
 	ok = load(),
 
+	%% context creation
 	Context = context(),
 	true = is_port(Context),
-	
+
+	%% type conversion
 	{ok, true} = script_result(Context, <<"true;">>),
-
-	{ok, true} = script_result(Context, <<"var f = function() { return true; }; f();">>),
-
-	{exception, {struct, ExProps}} = script_result(Context, <<"g();">>),
-	<<"g is not defined">> = proplists:get_value(<<"message">>, ExProps),
-	{ok, null} = script_result(Context, <<"var g = f; null;">>),
-	{ok, true} = script_result(Context, <<"g();">>),
-	
+	{ok, false} = script_result(Context, <<"false;">>),
+	{ok, null} = script_result(Context, <<"null;">>),
+	{ok, undefined} = script_result(Context, <<"undefined;">>),
+	{ok, undefined} = script_result(Context, <<"var foo = function() {};">>),
+	{ok, []} = script_result(Context, <<"[];">>),
 	{ok, [1, 2, 3]} = script_result(Context, <<"[1, 2, 3];">>),
-
 	{ok, [1, true, 3, [4, false]]} = script_result(Context, <<"[1, true, 3, [4, false]];">>),
-	
 	{ok, {struct, ObjProps}} = script_result(Context, <<"var o = { 'foo': true, 'bar': false }; o;">>),
 	2 = length(ObjProps),
 	true = proplists:get_value(<<"foo">>, ObjProps),
 	false = proplists:get_value(<<"bar">>, ObjProps),
 
+	%% functions and context
+	{ok, true} = script_result(Context, <<"var f = function() { return true; }; f();">>),
+	{exception, {struct, ExProps}} = script_result(Context, <<"g();">>),
+	<<"g is not defined">> = proplists:get_value(<<"message">>, ExProps),
+	{ok, null} = script_result(Context, <<"var g = f; null;">>),
+	{ok, true} = script_result(Context, <<"g();">>),
+
+	%% context destruction
 	true = destroy(Context),
 
-	io:format("pass~n"),
+	io:format("[v8]: all tests pass~n"),
 	true
     catch
 	T:E ->
-	    io:format("fail: ~w: ~w~n", [T, E]),
+	    io:format("[v8] test failure: ~w: ~w~n", [T, E]),
 	    false
     end.
     

File src/driver.cc

   const char *buf;
   long len;
   long key;
+  ErlDrvTermData caller;
   // output
   ErlDrvTermData type;
   Persistent<Value> result;
   buf = b;
   len = l;
   key = k;
+  caller = driver_caller(p);
 };
 
 ScriptRunner::~ScriptRunner() {
   terms[tlen - 2] = ERL_DRV_TUPLE;
   terms[tlen - 1] = 2;
 
-  int ret = driver_output_term(port, terms, tlen);
+  int ret = driver_send_term(port, caller, terms, tlen);
 
   for (list<void *>::iterator iter = pl.begin() ; iter != pl.end(); ++iter) {
     driver_free(*iter);
     return;
   }
 
-  char *script;
   int type;
   long script_length;
   if (ei_get_type(buf, &index, &type, reinterpret_cast<int *>(&script_length))) {
     LOG("[drv] cannot read size\n");
     return;
   }
-  script = new char[script_length];
+
+  char *script = new char[script_length];
   if (ei_decode_binary(buf, &index, script, &script_length)) {
     LOG("[drv] bad script binary\n");
     delete script;

File src/js2erl.cc

 #include "js2erl.h"
 #include <string.h>
 
+#define TERMS(terms, tlen) (terms != NULL ? (terms + tlen) : NULL)
+
 // internal
 static int date_to_terms(Handle<Date> val, ErlDrvTermData *terms, list<void *> *pl);
 static int object_to_terms(Handle<Object> obj, ErlDrvTermData *terms, list<void *> *pl);
 
   int alen = arr->Length();
   for (int i = 0; i < alen; ++i) {
-    tlen += value_to_terms(arr->Get(Integer::New(i)), terms != NULL ? (terms + tlen) : NULL, pl);
+    tlen += value_to_terms(arr->Get(Integer::New(i)), TERMS(terms, tlen), pl);
   }
-
+  
   if (terms != NULL) {
     terms[tlen] = ERL_DRV_NIL;
-    terms[tlen + 1] = ERL_DRV_LIST;
-    terms[tlen + 2] = alen + 1;
   }
-  tlen += 3;
+  tlen++;
 
+  if (alen > 0) {
+    if (terms != NULL) {
+      terms[tlen] = ERL_DRV_LIST;
+      terms[tlen + 1] = alen + 1;
+    }
+    tlen += 2;
+  }
+  
   return tlen;
 }
 
   //printf("[v2t] object_to_terms\n");
   int tlen = 0;
 
-  tlen += atom("struct", terms != NULL ? (terms + tlen) : NULL);
+  tlen += atom("struct", TERMS(terms, tlen));
   
   Handle<Array> keys = obj->GetPropertyNames();
   int olen = keys->Length();
   for (int i=0; i<olen; ++i) {
     Handle<Value> key = keys->Get(Integer::New(i));
-    tlen += string_to_terms(Handle<String>::Cast(key), terms != NULL ? (terms + tlen) : NULL, pl);
-    tlen += value_to_terms(obj->Get(key), terms != NULL ? (terms + tlen) : NULL, pl);
+    tlen += string_to_terms(Handle<String>::Cast(key), TERMS(terms, tlen), pl);
+    tlen += value_to_terms(obj->Get(key), TERMS(terms, tlen), pl);
     
     if (terms != NULL) {
       terms[tlen] = ERL_DRV_TUPLE;
     terms[tlen] = ERL_DRV_NIL;
     terms[tlen + 1] = ERL_DRV_LIST;
     terms[tlen + 2] = olen + 1;
-    terms[tlen + 3] = ERL_DRV_TUPLE;
+    terms[tlen + 3] = ERL_DRV_TUPLE; // {struct, [{<<"key">>, Value}, ...]}
     terms[tlen + 4] = 2;
   }
   tlen += 5;