Commits

jer...@jeremylatt.com  committed 17f1686

hooray, builds on osx

  • Participants
  • Parent commits d36beba

Comments (0)

Files changed (4)

 CPP := g++
 ARCH := $(shell uname -m | tr -s " ") #x86_64, x86, ppc, ...
 KERNEL := $(shell uname -s | tr -s " ") #Linux, Darwin, ...
+LIB_EXT := so
 
 #Build environment pointers
 LIB_DIR := ./lib
 
 #OS specific settings. 
 ifeq ($(strip $(KERNEL)),Darwin)
-LIB_EXT = dylib
-LDFLAGS = -Wall -fPIC -dynamiclib -flat_namespace -undefined suppress
+LDFLAGS = -Wall -fPIC -shared -flat_namespace -undefined suppress -m32
 else
-LIB_EXT = so
 LDFLAGS = -Wall -fPIC -shared -flat_namespace -undefined
 endif
 
 
 #Libs to build
 LIBS = v8erl
-LIB_TARGETS = $(addsuffix .$(LIB_EXT),$(addprefix lib/lib,$(LIBS)))
+LIB_TARGETS = $(addsuffix .$(LIB_EXT),$(addprefix lib/,$(LIBS)))
 
 #Beams to build
 ERL_FILES = $(wildcard $(ERL_DIR)/*.erl)
 JSC_TARGETS = $(addprefix js/,$(JSC_FILES))
 
 #Compile flags
-CXXFLAGS  = -I./include $(V8_INC) $(ERL_INC) -g -Wall
+CXXFLAGS  = -I./include $(V8_INC) $(ERL_INC) -g -Wall -m32
 
 default: all
 
 $(SRC_DIR)/%.o: $(SRC_DIR)/%.cc
 	$(CPP) -c $(CXXFLAGS) -o $@ $<
 
-lib/libv8erl.so: CXXFLAGS += -fPIC
-lib/libv8erl.so: $(SRC_DIR)/driver.o $(SRC_DIR)/js2erl.o
-	$(CPP) $(LDFLAGS) $^ -o $@
-
-lib/libv8erl.dylib: CXXFLAGS += -fPIC
-lib/libv8erl.dylib: $(SRC_DIR)/driver.o $(SRC_DIR)/js2erl.o
+lib/v8erl.so: CXXFLAGS += -fPIC
+lib/v8erl.so: $(SRC_DIR)/driver.o $(SRC_DIR)/js2erl.o libv8.a
 	$(CPP) $(LDFLAGS) $^ -o $@
 
 $(EBIN_DIR)/%.beam: $(ERL_DIR)/%.erl
 	cp $< $@
 
 clean:    
-	rm -f src/*.o
-	rm -rf $(LIB_DIR)
-	rm -rf $(EBIN_DIR)
-	rm -f $(JSC_TARGETS)
+	rm -rf src/*.o js/*.jsc $(LIB_DIR) $(EBIN_DIR)
 
-.PHONY: all clean erl localdirs
+test:
+	erl -pa ebin -run v8 load
+
+.PHONY: all clean erl localdirs test
 
 js/%.jsc: js/%.js
 	js -s -C $<
 -module(v8).
 -export([load/0,
-		 context/0,
-		 script/2]).
+         context/0,
+         script/2]).
 -define(LIB, "v8erl").
 
 load() ->
     case whereis(v8erl) of
-		undefined ->
-			case erl_ddll:load_driver(".", ?LIB) of
-				ok ->
-					ok;
-				{error, already_loaded} ->
-					ok;
-				E -> 
-					exit(E)
-			end;
-		_ ->
-			ok
+        undefined ->
+            case erl_ddll:load_driver("lib", ?LIB) of
+                ok ->
+                    ok;
+                {error, already_loaded} ->
+                    ok;
+                {error, Err} ->
+		    {error, erl_ddll:format_error(Err)}
+            end;
+        _ ->
+            ok
     end.
 
 
 
 
 script(Context, Script) when is_port(Context) andalso 
-							 is_binary(Script) ->
-    port_control(Context, 1, Script).
+                             is_binary(Script) ->
+    Context ! Script,
+    receive
+	{Context, {data, Data}} ->
+	    Data;
+	{Context, closed} ->
+	    closed
+    end.

File src/driver.cc

 static void run_script(void *);
 static void async_free(void *);
 
+static char COMPILER_ERROR[] = "compiler_error";
+static char DRIVER_NAME[] = "v8erl";
+
 class V8Data {
 public:
   ErlDrvPort port;
 };
 
 V8Data::V8Data(ErlDrvPort p, Handle<Context> c) {
+  HandleScope handle_scope;
   port = p;
   context = Persistent<Context>::New(c);
 };
 
 AsyncData::AsyncData(Handle<Context> c, char *b, int l) {
   context = c;
-  buf     = b;
-  len     = l;
+  buf = b;
+  len = l;
 };
 
 AsyncData::~AsyncData() {
 };
 
 void AsyncData::setResult(Handle<Value> result) {
-  tlen  = terms_length(result);
+  tlen = terms_length(result);
   terms = (ErlDrvTermData *)driver_alloc(sizeof(ErlDrvTermData) * tlen);
   value_to_terms(result, terms);
 };
   return reinterpret_cast<AsyncData *>(data);
 };
 
-//
-//
-//
-
-ErlDrvEntry driver_entry = {
+static ErlDrvEntry driver_entry = {
   init, // init driver
   start, // open_port
   stop, // close_port
   output, // output		
-  NULL,			/* ready_input */
-  NULL,			/* ready_output */ 
-  "v8erl", // driver name
-  finish,  // unload dynamic driver
+  NULL, // ready_input
+  NULL, // ready_output
+  DRIVER_NAME, // driver name
+  finish, // unload dynamic driver
   NULL, // handle: EMULATOR USE ONLY
-  NULL,			/* control */
-  NULL,			/* timeout */
-  NULL,			/* outputv */
+  NULL, // control
+  NULL, // timeout
+  NULL, // outputv
   ready_async, // async event done
-  NULL,			/* flush */
-  NULL,			/* call */
-  NULL			/* event */
+  NULL, // flush
+  NULL, // call
+  NULL // event
 };
 
-extern "C" { 
-  DRIVER_INIT(v8erl) {
-    return &driver_entry;
-  }
+extern "C" DRIVER_INIT(v8erl) {
+  return &driver_entry;
 }
 
 Persistent<ObjectTemplate> erl_global() {
+  HandleScope handle_scope;
   Handle<ObjectTemplate> global = ObjectTemplate::New(); 
   return Persistent<ObjectTemplate>::New(global);
 }
 
 Persistent<ObjectTemplate> erl_term_template() {
+  HandleScope handle_scope;
   Handle<ObjectTemplate> temp = ObjectTemplate::New();
   temp->SetInternalFieldCount(1);
   return Persistent<ObjectTemplate>::New(temp);
 static ErlDrvData start(ErlDrvPort port, char *cmd) {
   if (port == NULL) { 
     return ERL_DRV_ERROR_GENERAL; // why?
-  } else { 
+  } else {
+    HandleScope handle_scope;
     Handle<Context> context = Context::New(NULL, global_template);
     V8Data *data = new V8Data(port, context);
     return data->castOut(); 
   delete V8Data::castIn(edata);
 }
 
-
 static void output(ErlDrvData edata, char *buf, int len) {
-  V8Data     *data = V8Data::castIn(edata);
+  V8Data *data = V8Data::castIn(edata);
   AsyncData *adata = new AsyncData(data->context, buf, len);
   driver_async(data->port, 
                (unsigned int *)&(data->port), // per-port async
                async_free);
 }
 
-
 static void ready_async(ErlDrvData edata, ErlDrvThreadData tdata) {
-  V8Data     *data = V8Data::castIn(edata);
+  V8Data *data = V8Data::castIn(edata);
   AsyncData *adata = AsyncData::castIn(tdata);
   adata->send(data->port);
   delete adata;
 }
 
-
 static void run_script(void *tdata) {
   AsyncData *adata = AsyncData::castIn(tdata);
  
   Handle<String> script = String::New(adata->buf, adata->len);
   Handle<Script> compiled = Script::Compile(script);
   if (compiled.IsEmpty()) {
-    adata->setError("compiler_error"); 
+    adata->setError(COMPILER_ERROR); 
   } else { 
     adata->setResult(compiled->Run()); 
   }
 }
 
-
 static void async_free(void *tdata) {
   delete AsyncData::castIn(tdata);
 }