Commits

Anonymous committed 56fdeca

do lists right

Comments (0)

Files changed (4)

 
 all: localdirs $(LIB_TARGETS) $(BEAM_TARGETS) $(APP_TARGETS) $(JSC_TARGETS)
 
-$(SRC_DIR)/%.o: $(SRC_DIR)/%.cc
+$(SRC_DIR)/%.o: $(SRC_DIR)/%.cc include/js2erl.h
 	$(CPP) -c $(CXXFLAGS) -o $@ $<
 
 lib/v8erl.so: CXXFLAGS += -fPIC
     erlang:port_command(Context, term_to_binary({Key, Script})),
     Key.
 
-print_script_result(Context, Script) ->
-    io:format("[erl] script: ~s~n", [Script]),
+
+script_result(Context, Script) ->
+    io:format("~s~n", [Script]),
     Key = execute(Context, Script),
     receive
 	{Key, Response} ->
-	    case Response of
-		{ok, Result} ->
-		    io:format("[erl] result: ~w~n", [Result]);
-		{error, Exception} ->
-		    {struct, PList} = Exception,
-		    io:format("[erl] error: ~s~n", [proplists:get_value(<<"message">>, PList)])
-	    end
+	    Response
     after
 	3000 ->
-	    io:format("[erl] timeout~n", [])
+	    {error, timeout}
     end.
 
 test() ->
-    io:format("[erl] load ~w~n", [load()]),
-    Context = context(),
-    print_script_result(Context, <<"true;">>),
-    print_script_result(Context, <<"var f = function() { return true; }; f();">>),
-    print_script_result(Context, <<"g();">>),
-    print_script_result(Context, <<"var g = f; null;">>),
-    print_script_result(Context, <<"g();">>),
-    print_script_result(Context, <<"[1, 2, 3];">>),
-    print_script_result(Context, <<"var o = { 'foo': true, 'bar': false }; o;">>),
-    destroy(Context),
-    ok.
+    try 
+	ok = load(),
+
+	Context = context(),
+	true = is_port(Context),
+	
+	{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, [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),
+
+	true = destroy(Context),
+
+	io:format("pass~n"),
+	true
+    catch
+	T:E ->
+	    io:format("fail: ~w: ~w~n", [T, E]),
+	    false
+    end.
     
 
 static char DRIVER_NAME[] = "v8erl";
 static const ErlDrvTermData OK = v8erl_driver_mk_atom("ok");
-static const ErlDrvTermData ERROR = v8erl_driver_mk_atom("error");
+static const ErlDrvTermData EXCEPTION = v8erl_driver_mk_atom("exception");
 
 static Persistent<ObjectTemplate> global_template;
 
   Handle<String> script = String::New(script_runner->buf, script_runner->len);
   Handle<Script> compiled = Script::Compile(script);
   if (compiled.IsEmpty()) {
-    script_runner->setResult(ERROR, try_catch.Exception());
+    script_runner->setResult(EXCEPTION, try_catch.Exception());
   } else {
     Handle<Value> value = compiled->Run();
     if (value.IsEmpty()) {
-      script_runner->setResult(ERROR, try_catch.Exception());
+      script_runner->setResult(EXCEPTION, try_catch.Exception());
     } else {
       script_runner->setResult(OK, value);
     }
   int tlen = 0;
 
   int alen = arr->Length();
-  for (int i=0; i<alen; ++i) {
+  for (int i = 0; i < alen; ++i) {
     tlen += value_to_terms(arr->Get(Integer::New(i)), terms != NULL ? (terms + tlen) : NULL, pl);
   }
 
   if (terms != NULL) {
-    terms[tlen] = ERL_DRV_LIST;
-    terms[tlen + 1] = alen;
+    terms[tlen] = ERL_DRV_NIL;
+    terms[tlen + 1] = ERL_DRV_LIST;
+    terms[tlen + 2] = alen + 1;
   }
-  tlen += 2;
+  tlen += 3;
 
   return tlen;
 }
   }
 
   if (terms != NULL) {
-    terms[tlen] = ERL_DRV_LIST;
-    terms[tlen + 1] = olen;
-    terms[tlen + 2] = ERL_DRV_TUPLE;
-    terms[tlen + 3] = 2;
+    terms[tlen] = ERL_DRV_NIL;
+    terms[tlen + 1] = ERL_DRV_LIST;
+    terms[tlen + 2] = olen + 1;
+    terms[tlen + 3] = ERL_DRV_TUPLE;
+    terms[tlen + 4] = 2;
   }
-  tlen += 4;
+  tlen += 5;
 
   return tlen;
 }
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.