jlong JavaJSImpl::functionCreate( const char * code ) { jstring s = _getEnv()->NewStringUTF( code ); jassert( s ); jlong id = _getEnv()->CallStaticLongMethod( _dbhook , _functionCreate , s ); _getEnv()->DeleteLocalRef( s ); return id; }
int JavaJSImpl::scopeSetString( jlong id , const char * field , const char * val ) { jstring s1 = _getEnv()->NewStringUTF( field ); jstring s2 = _getEnv()->NewStringUTF( val ); int res = _getEnv()->CallStaticBooleanMethod( _dbhook , _scopeSetString , id , s1 , s2 ); _getEnv()->DeleteLocalRef( s1 ); _getEnv()->DeleteLocalRef( s2 ); return res; }
void JavaJSImpl::printException() { jthrowable exc = _getEnv()->ExceptionOccurred(); if ( exc ) { _getEnv()->ExceptionDescribe(); _getEnv()->ExceptionClear(); } }
int JavaJSImpl::scopeSetThis( jlong id , const BSONObj * obj ) { if ( ! obj ) return 0; jobject bb = _getEnv()->NewDirectByteBuffer( (void*)(obj->objdata()) , (jlong)(obj->objsize()) ); jassert( bb ); int res = _getEnv()->CallStaticBooleanMethod( _dbhook , _scopeSetThis , id , bb ); _getEnv()->DeleteLocalRef( bb ); return res; }
void JavaJSImpl::run( const char * js ) { jclass c = findClass( "ed/js/JS" ); jassert( c ); jmethodID m = _getEnv()->GetStaticMethodID( c , "eval" , "(Ljava/lang/String;)Ljava/lang/Object;" ); jassert( m ); jstring s = _getEnv()->NewStringUTF( js ); log() << _getEnv()->CallStaticObjectMethod( c , m , s ) << endl; _getEnv()->DeleteLocalRef( s ); }
string JavaJSImpl::scopeGetString( jlong id , const char * field ) { jstring s1 = _getEnv()->NewStringUTF( field ); jstring s = (jstring)_getEnv()->CallStaticObjectMethod( _dbhook , _scopeGetString , id , s1 ); _getEnv()->DeleteLocalRef( s1 ); if ( ! s ) return ""; const char * c = _getEnv()->GetStringUTFChars( s , 0 ); string retStr(c); _getEnv()->ReleaseStringUTFChars( s , c ); return retStr; }
int JavaJSImpl::scopeSetObject( jlong id , const char * field , const BSONObj * obj ) { jobject bb = 0; if ( obj ) { bb = _getEnv()->NewDirectByteBuffer( (void*)(obj->objdata()) , (jlong)(obj->objsize()) ); jassert( bb ); } jstring s1 = _getEnv()->NewStringUTF( field ); int res = _getEnv()->CallStaticBooleanMethod( _dbhook , _scopeSetObject , id , s1 , bb ); _getEnv()->DeleteLocalRef( s1 ); if ( bb ) _getEnv()->DeleteLocalRef( bb ); return res; }
JNIEnv* PluginJniHelper::getEnv() { JNIEnv* ret = NULL; bool bRet = _getEnv(&ret); if (! bRet) ret = NULL; return ret; }
void cWaterUberShader::registHandle() { m_handle = new D3DXHANDLE[H_NUM]; ZeroMemory(m_handle, sizeof (D3DXHANDLE) * H_NUM); /// base m_handle[H_TUBER] = insertTechnique(_T("TUber")); m_handle[H_MAT_WORLD] = insertParameter(_T("matWorld")); m_handle[H_MAT_WVP] = insertParameter(_T("matWVP")); m_handle[H_AMBIENT] = insertParameter(_T("ambient")); /// m_handle[H_TIMER] = insertParameter(_T("time")); /// texture m_handle[H_DIFFUSE_TEXTURE] = insertParameter(_T("diffuseTexture")); m_handle[H_NORMAL_TEXTURE] = insertParameter(_T("normalTexture")); m_handle[H_ENVIRONMENT_TEXTURE] = insertParameter(_T("environmentTexture")); // fog if (_getEnv()->getFog()->getEnable()) m_handle[H_FOG] = insertParameter(_T("fog")); insertParameter(_T("textureScale")); insertParameter(_T("waveFreq")); insertParameter(_T("waveAmp")); insertParameter(_T("bumpScale")); insertParameter(_T("bumpSpeed")); insertParameter(_T("deepColor")); insertParameter(_T("shallowColor")); insertParameter(_T("waterAmount")); insertParameter(_T("reflectionColor")); insertParameter(_T("reflectionAmount")); insertParameter(_T("reflectionBlur")); insertParameter(_T("fresnelPower")); insertParameter(_T("fresnelBias")); insertParameter(_T("hdrMultiplier")); }
BSONObj JavaJSImpl::scopeGetObject( jlong id , const char * field ) { jstring s1 = _getEnv()->NewStringUTF( field ); int guess = _getEnv()->CallStaticIntMethod( _dbhook , _scopeGuessObjectSize , id , _getEnv()->NewStringUTF( field ) ); _getEnv()->DeleteLocalRef( s1 ); if ( guess == 0 ) return BSONObj(); char * buf = (char *) malloc(guess); jobject bb = _getEnv()->NewDirectByteBuffer( (void*)buf , guess ); jassert( bb ); int len = _getEnv()->CallStaticIntMethod( _dbhook , _scopeGetObject , id , _getEnv()->NewStringUTF( field ) , bb ); _getEnv()->DeleteLocalRef( bb ); jassert( len > 0 && len < guess ); BSONObj obj(buf, true); assert( obj.objsize() <= guess ); return obj; }
char JavaJSImpl::scopeGetType( jlong id , const char * field ) { jstring s1 = _getEnv()->NewStringUTF( field ); int res =_getEnv()->CallStaticByteMethod( _dbhook , _scopeGetType , id , s1 ); _getEnv()->DeleteLocalRef( s1 ); return res; }
jboolean JavaJSImpl::scopeReset( jlong id ) { return _getEnv()->CallStaticBooleanMethod( _dbhook , _scopeReset ); }
int JavaJSImpl::invoke( jlong scope , jlong function ) { return _getEnv()->CallStaticIntMethod( _dbhook , _invoke , scope , function ); }
void JavaJSImpl::scopeFree( jlong id ) { _getEnv()->CallStaticVoidMethod( _dbhook , _scopeFree , id ); }
int JavaJSImpl::scopeSetNumber( jlong id , const char * field , double val ) { jstring fieldString = _getEnv()->NewStringUTF( field ); int res = _getEnv()->CallStaticBooleanMethod( _dbhook , _scopeSetNumber , id , fieldString , val ); _getEnv()->DeleteLocalRef( fieldString ); return res; }
jboolean JavaJSImpl::scopeGetBoolean( jlong id , const char * field ) { jstring s1 = _getEnv()->NewStringUTF( field ); jboolean res = _getEnv()->CallStaticBooleanMethod( _dbhook , _scopeGetBoolean , id , s1 ); _getEnv()->DeleteLocalRef( s1 ); return res; }
jlong JavaJSImpl::scopeCreate() { return _getEnv()->CallStaticLongMethod( _dbhook , _scopeCreate ); }
double JavaJSImpl::scopeGetNumber( jlong id , const char * field ) { jstring s1 = _getEnv()->NewStringUTF( field ); double res = _getEnv()->CallStaticDoubleMethod( _dbhook , _scopeGetNumber , id , s1 ); _getEnv()->DeleteLocalRef( s1 ); return res; }
JavaJSImpl::JavaJSImpl(const char *appserverPath) { _jvm = 0; _mainEnv = 0; _dbhook = 0; stringstream ss; string edTemp; const char * ed = 0; ss << "-Djava.class.path=."; if ( appserverPath ) { ed = findEd(appserverPath); assert( ed ); ss << SYSTEM_COLON << ed << "/build/"; _addClassPath( ed , ss , "include" ); _addClassPath( ed , ss , "include/jython/" ); _addClassPath( ed , ss , "include/jython/javalib" ); } else { const string jars = findJars(); _addClassPath( jars.c_str() , ss , "jars" ); edTemp += (string)jars + "/jars/mongojs-js.jar"; ed = edTemp.c_str(); } #if defined(_WIN32) ss << SYSTEM_COLON << "C:\\Program Files\\Java\\jdk\\lib\\tools.jar"; #else ss << SYSTEM_COLON << "/opt/java/lib/tools.jar"; #endif if ( getenv( "CLASSPATH" ) ) ss << SYSTEM_COLON << getenv( "CLASSPATH" ); string s = ss.str(); char * p = (char *)malloc( s.size() * 4 ); strcpy( p , s.c_str() ); char *q = p; #if defined(_WIN32) while ( *p ) { if ( *p == '/' ) *p = '\\'; p++; } #endif log(1) << "classpath: " << q << endl; JavaVMOption * options = new JavaVMOption[4]; options[0].optionString = q; options[1].optionString = (char*)"-Djava.awt.headless=true"; options[2].optionString = (char*)"-Xmx300m"; // Prevent JVM from using async signals internally, since on linux the pre-installed handlers for these // signals don't seem to be respected by JNI. options[3].optionString = (char*)"-Xrs"; // -Xcheck:jni _vmArgs = new JavaVMInitArgs(); _vmArgs->version = JNI_VERSION_1_4; _vmArgs->options = options; _vmArgs->nOptions = 4; _vmArgs->ignoreUnrecognized = JNI_FALSE; log(1) << "loading JVM" << endl; jint res = JNI_CreateJavaVM( &_jvm, (void**)&_mainEnv, _vmArgs ); if ( res ) { log() << "using classpath: " << q << endl; log() << " res : " << (unsigned) res << " " << "_jvm : " << _jvm << " " << "_env : " << _mainEnv << " " << endl; problem() << "Couldn't create JVM res:" << (int) res << " terminating" << endl; log() << "(try --nojni if you do not require that functionality)" << endl; exit(22); } jassert( res == 0 ); jassert( _jvm > 0 ); jassert( _mainEnv > 0 ); _envs = new boost::thread_specific_ptr<JNIEnv>( myJNIClean ); assert( ! _envs->get() ); _envs->reset( _mainEnv ); _dbhook = findClass( "ed/db/JSHook" ); if ( _dbhook == 0 ) { log() << "using classpath: " << q << endl; printException(); } jassert( _dbhook ); if ( ed ) { jmethodID init = _mainEnv->GetStaticMethodID( _dbhook , "init" , "(Ljava/lang/String;)V" ); jassert( init ); _mainEnv->CallStaticVoidMethod( _dbhook , init , _getEnv()->NewStringUTF( ed ) ); } _dbjni = findClass( "ed/db/DBJni" ); jassert( _dbjni ); _scopeCreate = _mainEnv->GetStaticMethodID( _dbhook , "scopeCreate" , "()J" ); _scopeInit = _mainEnv->GetStaticMethodID( _dbhook , "scopeInit" , "(JLjava/nio/ByteBuffer;)Z" ); _scopeSetThis = _mainEnv->GetStaticMethodID( _dbhook , "scopeSetThis" , "(JLjava/nio/ByteBuffer;)Z" ); _scopeReset = _mainEnv->GetStaticMethodID( _dbhook , "scopeReset" , "(J)Z" ); _scopeFree = _mainEnv->GetStaticMethodID( _dbhook , "scopeFree" , "(J)V" ); _scopeGetNumber = _mainEnv->GetStaticMethodID( _dbhook , "scopeGetNumber" , "(JLjava/lang/String;)D" ); _scopeGetString = _mainEnv->GetStaticMethodID( _dbhook , "scopeGetString" , "(JLjava/lang/String;)Ljava/lang/String;" ); _scopeGetBoolean = _mainEnv->GetStaticMethodID( _dbhook , "scopeGetBoolean" , "(JLjava/lang/String;)Z" ); _scopeGetType = _mainEnv->GetStaticMethodID( _dbhook , "scopeGetType" , "(JLjava/lang/String;)B" ); _scopeGetObject = _mainEnv->GetStaticMethodID( _dbhook , "scopeGetObject" , "(JLjava/lang/String;Ljava/nio/ByteBuffer;)I" ); _scopeGuessObjectSize = _mainEnv->GetStaticMethodID( _dbhook , "scopeGuessObjectSize" , "(JLjava/lang/String;)J" ); _scopeSetNumber = _mainEnv->GetStaticMethodID( _dbhook , "scopeSetNumber" , "(JLjava/lang/String;D)Z" ); _scopeSetBoolean = _mainEnv->GetStaticMethodID( _dbhook , "scopeSetBoolean" , "(JLjava/lang/String;Z)Z" ); _scopeSetString = _mainEnv->GetStaticMethodID( _dbhook , "scopeSetString" , "(JLjava/lang/String;Ljava/lang/String;)Z" ); _scopeSetObject = _mainEnv->GetStaticMethodID( _dbhook , "scopeSetObject" , "(JLjava/lang/String;Ljava/nio/ByteBuffer;)Z" ); _functionCreate = _mainEnv->GetStaticMethodID( _dbhook , "functionCreate" , "(Ljava/lang/String;)J" ); _invoke = _mainEnv->GetStaticMethodID( _dbhook , "invoke" , "(JJ)I" ); jassert( _scopeCreate ); jassert( _scopeInit ); jassert( _scopeSetThis ); jassert( _scopeReset ); jassert( _scopeFree ); jassert( _scopeGetNumber ); jassert( _scopeGetString ); jassert( _scopeGetObject ); jassert( _scopeGetBoolean ); jassert( _scopeGetType ); jassert( _scopeGuessObjectSize ); jassert( _scopeSetNumber ); jassert( _scopeSetBoolean ); jassert( _scopeSetString ); jassert( _scopeSetObject ); jassert( _functionCreate ); jassert( _invoke ); JNINativeMethod * nativeSay = new JNINativeMethod(); nativeSay->name = (char*)"native_say"; nativeSay->signature = (char*)"(Ljava/nio/ByteBuffer;)V"; nativeSay->fnPtr = (void*)java_native_say; _mainEnv->RegisterNatives( _dbjni , nativeSay , 1 ); JNINativeMethod * nativeCall = new JNINativeMethod(); nativeCall->name = (char*)"native_call"; nativeCall->signature = (char*)"(Ljava/nio/ByteBuffer;Ljava/nio/ByteBuffer;)I"; nativeCall->fnPtr = (void*)java_native_call; _mainEnv->RegisterNatives( _dbjni , nativeCall , 1 ); }