Skip to content

Commit

Permalink
compiles and works on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
joeferner committed Jan 22, 2012
1 parent 191cbc9 commit 0128b73
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 13 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,23 @@ $ export JDK_LIB_DIR=/usr/local/share/jdk1.6.0_30/jre/lib/i386/client/
$ npm install java
```

## Installation Windows

* Install Python
* Download node.js source
* Run a Visual Studios command prompt
* Build node.js source by running "vcbuild.bat release"
* The directory where jvm.dll exists must be in the PATH.

```bash
$ set NODE_ROOT=C:\Program Files (x86)\nodejs
$ vcbuild.bat
```

## Installation Mac

```bash
$ ???
$ npm install java
```

Expand Down
6 changes: 5 additions & 1 deletion src/java.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,11 @@ v8::Handle<v8::Value> Java::createJVM(JavaVM** jvm, JNIEnv** env) {
v8::Local<v8::Array> classPathArray = v8::Array::Cast(*classPathValue);
for(uint32_t i=0; i<classPathArray->Length(); i++) {
if(i != 0) {
classPath << ":"; // TODO: figure out path seperator
#ifdef WIN32
classPath << ";";
#else
classPath << ":";
#endif
}
v8::Local<v8::Value> arrayItemValue = classPathArray->Get(i);
if(!arrayItemValue->IsString()) {
Expand Down
12 changes: 6 additions & 6 deletions src/methodCallBaton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ MethodCallBaton::~MethodCallBaton() {
}

void MethodCallBaton::run() {
eio_custom(MethodCallBaton::EIO_MethodCall, EIO_PRI_DEFAULT, MethodCallBaton::EIO_AfterMethodCall, this);
ev_ref(EV_DEFAULT_UC);
uv_work_t* req = new uv_work_t();
req->data = this;
uv_queue_work(uv_default_loop(), req, MethodCallBaton::EIO_MethodCall, MethodCallBaton::EIO_AfterMethodCall);
}

v8::Handle<v8::Value> MethodCallBaton::runSync() {
Expand All @@ -32,20 +33,19 @@ v8::Handle<v8::Value> MethodCallBaton::runSync() {
return resultsToV8(env);
}

/*static*/ void MethodCallBaton::EIO_MethodCall(eio_req* req) {
/*static*/ void MethodCallBaton::EIO_MethodCall(uv_work_t* req) {
MethodCallBaton* self = static_cast<MethodCallBaton*>(req->data);
JNIEnv *env = javaAttachCurrentThread(self->m_java->getJvm());
self->execute(env);
javaDetachCurrentThread(self->m_java->getJvm());
}

/*static*/ int MethodCallBaton::EIO_AfterMethodCall(eio_req* req) {
/*static*/ void MethodCallBaton::EIO_AfterMethodCall(uv_work_t* req) {
MethodCallBaton* self = static_cast<MethodCallBaton*>(req->data);
JNIEnv *env = self->m_java->getJavaEnv();
self->after(env);
ev_unref(EV_DEFAULT_UC);
delete req;
delete self;
return 0;
}

void MethodCallBaton::after(JNIEnv *env) {
Expand Down
6 changes: 3 additions & 3 deletions src/methodCallBaton.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
#ifndef _methodcallbaton_h_
#define _methodcallbaton_h_

#include "utils.h"
#include <v8.h>
#include <node.h>
#include <jni.h>
#include <list>
#include "utils.h"

class Java;
class JavaObject;
Expand All @@ -16,8 +16,8 @@ class MethodCallBaton {
MethodCallBaton(Java* java, jobject method, jarray args, v8::Handle<v8::Value>& callback);
virtual ~MethodCallBaton();

static void EIO_MethodCall(eio_req* req);
static int EIO_AfterMethodCall(eio_req* req);
static void EIO_MethodCall(uv_work_t* req);
static void EIO_AfterMethodCall(uv_work_t* req);
void run();
v8::Handle<v8::Value> runSync();

Expand Down
8 changes: 8 additions & 0 deletions src/nodeJavaBridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,11 @@ extern "C" {

NODE_MODULE(nodejavabridge_bindings, init);
}

#ifdef WIN32

BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) {
return TRUE;
}

#endif
7 changes: 4 additions & 3 deletions src/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ std::string javaToString(JNIEnv *env, jstring str) {

std::string javaObjectToString(JNIEnv *env, jobject obj) {
if(obj == NULL) {
return "";
return "(null)";
}
jclass objClazz = env->GetObjectClass(obj);
jmethodID methodId = env->GetMethodID(objClazz, "toString", "()Ljava/lang/String;");
Expand Down Expand Up @@ -161,8 +161,9 @@ void javaDetachCurrentThread(JavaVM* jvm) {

jvalueType javaGetType(JNIEnv *env, jclass type) {
// TODO: has to be a better way
const char *typeStr = javaObjectToString(env, type).c_str();
//printf("%s\n", typeStr);
std::string str = javaObjectToString(env, type);
const char *typeStr = str.c_str();
//printf("javaGetType: %s\n", typeStr);
if(strcmp(typeStr, "int") == 0) {
return TYPE_INT;
} else if(strcmp(typeStr, "long") == 0) {
Expand Down
1 change: 1 addition & 0 deletions src/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#ifndef _utils_h_
#define _utils_h_

#define BUILDING_NODE_EXTENSION 1
#include <v8.h>
#include <jni.h>
#include <list>
Expand Down
59 changes: 59 additions & 0 deletions vcbuild.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
@echo OFF

echo Check for nodejs build location variable: %NODE_ROOT%
if not defined NODE_ROOT goto nodebuild-not-found
if not exist "%NODE_ROOT%\src\node.h" goto nodebuild-not-found
if not exist "%NODE_ROOT%\deps\v8\include\v8.h" goto nodebuild-not-found
if not exist "%NODE_ROOT%\deps\uv\include\uv.h" goto nodebuild-not-found

echo detect the location of the node.lib file
set nodelibpath=
if exist "%NODE_ROOT%\Release\node.lib" set nodelibpath=%NODE_ROOT%\Release
if not defined nodelibpath if exist "%NODE_ROOT%\Debug\node.lib" set nodelibpath=%NODE_ROOT%\Debug
if not defined nodelibpath goto nodebuild-not-found

echo detect java
if not defined NODE_ROOT goto java-not-found
if not exist "%JAVA_HOME%\include\jni.h" goto java-not-found
if not exist "%JAVA_HOME%\lib\jvm.lib" goto java-not-found

echo Check for visual studio tools if not already loaded
if defined VCINSTALLDIR goto start-compilation
if not defined VS100COMNTOOLS goto msbuild-not-found
if not exist "%VS100COMNTOOLS%..\..\vc\vcvarsall.bat" goto msbuild-not-found
call "%VS100COMNTOOLS%..\..\vc\vcvarsall.bat"
if not defined VCINSTALLDIR goto msbuild-not-found

:start-compilation
echo Compiling...
@rem "support throws" "don't strip comments" "no banner" "disable intrinsic functions" "no optimization" "calling conversion __cdecl" "no analysis" "the /I adds some folders in the include path"
rem /ZI /nologo /W3 /WX- /Od /Oy- /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /D "_USRDLL" /D "NODEJAVA_EXPORTS" /D "_WINDLL" /D "_UNICODE" /D "UNICODE" /Gm /EHsc /RTC1 /GS /fp:precise /Zc:wchar_t /Zc:forScope /Yu"StdAfx.h" /Fp"Debug\node-java.pch" /Fa"Debug\" /Fo"Debug\" /Fd"Debug\vc100.pdb" /Gd /analyze- /errorReport:queue
cl.exe src\*.cpp /D "WIN32" /D "_WINDOWS" /D "_WINDLL" /EHsc /c /nologo /Oi- /Od /Gd /analyze- /I "%NODE_ROOT%\deps\v8\include" /I "%NODE_ROOT%\src" /I "%NODE_ROOT%\deps\uv\include" /I "%JAVA_HOME%\include" /I "%JAVA_HOME%\include\win32"
if errorlevel 1 goto exit-error
echo Done compiling. Linking...
echo Using %nodelibpath%\node.lib file to link to
link *.obj node.lib jvm.lib uv.lib /OUT:"nodejavabridge_bindings.dll" /NOLOGO /DLL /MANIFEST:NO /SUBSYSTEM:WINDOWS /TLBID:1 /DYNAMICBASE /NXCOMPAT /MACHINE:X86 /LIBPATH:%nodelibpath% /LIBPATH:%nodelibpath%/lib /LIBPATH:"%JAVA_HOME%\lib"
if errorlevel 1 goto exit-error
echo Done linking
echo Cleaning up
if not exist build mkdir build
if not exist build\Release mkdir build\Release
move nodejavabridge_bindings.dll build\Release\nodejavabridge_bindings.node
echo Finished
goto exit

:msbuild-not-found
echo Visual studio tools were not found! Please check the VS100COMNTOOLS path variable
goto exit

:nodebuild-not-found
echo Node build path not found! Please check the NODE_ROOT path variable exists and that it points to the root of the git repo where you have build
goto exit

:java-not-found
echo Java not found! Please check JAVA_HOME variable.
goto exit

:exit-error
echo An error occured. Please check the above output
:exit

0 comments on commit 0128b73

Please sign in to comment.