-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e974a86
commit f384750
Showing
27 changed files
with
685 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
(function() { | ||
const $java = process._linkedBinding("java"); | ||
$java.constructors = {}; | ||
$java.prototypes = {}; | ||
|
||
$java.findClassOrNull = function (className) { | ||
const cachedClass = $java.constructors[className]; | ||
if (typeof cachedClass !== "undefined") { | ||
return cachedClass; | ||
} | ||
|
||
const classInfo = $java.getClassInfo(className); | ||
if (!classInfo) { | ||
return null; | ||
} | ||
|
||
return classInfo; | ||
} | ||
|
||
globalThis["$java"] = $java; | ||
})(); | ||
|
||
$java.setUnsafeReflectionEnabled(true); | ||
|
||
const clazz = $java.findClassOrNull("com.mucheng.nodejava.core.Context"); | ||
console.log(clazz); |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
#include "Locker.h" | ||
#include "Util.h" | ||
#include "Isolate.h" | ||
#include <jni.h> | ||
|
||
Locker::Locker(Isolate *isolate) { | ||
this->isolate = isolate; | ||
|
||
self = new v8::Locker(isolate->self); | ||
} | ||
|
||
void Locker::To(jobject instance, Locker *self) { | ||
Util::SetPtr(instance, "lockerPtr", self); | ||
} | ||
|
||
Locker *Locker::From(jobject instance) { | ||
return Util::GetPtrAs<Locker *>(instance, "lockerPtr"); | ||
} | ||
|
||
bool Locker::isLocked(Isolate *isolate) { | ||
return v8::Locker::IsLocked(isolate->self); | ||
} | ||
|
||
void Locker::release() { | ||
delete this->self; | ||
} | ||
|
||
extern "C" | ||
JNIEXPORT void JNICALL | ||
Java_com_mucheng_nodejava_core_Locker_nativeCreateLocker(JNIEnv *env, jobject thiz, | ||
jlong isolatePtr) { | ||
Isolate *isolate = Util::As<Isolate *>(isolatePtr); | ||
Locker *locker = new Locker(isolate); | ||
Locker::To(thiz, locker); | ||
} | ||
|
||
extern "C" | ||
JNIEXPORT jboolean JNICALL | ||
Java_com_mucheng_nodejava_core_Locker_nativeIsLocked(JNIEnv *env, jclass clazz, jlong isolatePtr) { | ||
Isolate *isolate = Util::As<Isolate *>(isolatePtr); | ||
return Locker::isLocked(isolate); | ||
} | ||
|
||
extern "C" | ||
JNIEXPORT jboolean JNICALL | ||
Java_com_mucheng_nodejava_core_Locker_nativeIsActive(JNIEnv *env, jobject thiz) { | ||
return Locker::From(thiz)->self->IsActive(); | ||
} | ||
|
||
extern "C" | ||
JNIEXPORT jboolean JNICALL | ||
Java_com_mucheng_nodejava_core_Locker_nativeWasEverUsed(JNIEnv *env, jobject thiz) { | ||
return Locker::From(thiz)->self->WasEverUsed(); | ||
} | ||
|
||
extern "C" | ||
JNIEXPORT void JNICALL | ||
Java_com_mucheng_nodejava_core_Locker_nativeRelease(JNIEnv *env, jobject thiz) { | ||
Locker::From(thiz)->release(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// | ||
// Created by 35785 on 2024/1/25. | ||
// | ||
|
||
#ifndef NODEJAVA_LOCKER_H | ||
#define NODEJAVA_LOCKER_H | ||
|
||
#include <jni.h> | ||
#include <v8.h> | ||
#include "Isolate.h" | ||
|
||
class Locker { | ||
public: | ||
Isolate *isolate; | ||
v8::Locker *self; | ||
|
||
Locker(Isolate *isolate); | ||
|
||
static void To(jobject instance, Locker *self); | ||
|
||
static Locker *From(jobject instance); | ||
|
||
void release(); | ||
|
||
static bool isLocked(Isolate *isolate); | ||
}; | ||
|
||
|
||
#endif //NODEJAVA_LOCKER_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#include "Unlocker.h" | ||
#include "Util.h" | ||
#include <v8.h> | ||
|
||
Unlocker::Unlocker(Isolate *isolate) { | ||
this->isolate = isolate; | ||
|
||
self = new v8::Unlocker(isolate->self); | ||
} | ||
|
||
void Unlocker::To(jobject instance, Unlocker *unlocker) { | ||
Util::SetPtr(instance, "unlockerPtr", unlocker); | ||
} | ||
|
||
Unlocker *Unlocker::From(jobject instance) { | ||
return Util::GetPtrAs<Unlocker *>(instance, "unlockerPtr"); | ||
} | ||
|
||
extern "C" | ||
JNIEXPORT void JNICALL | ||
Java_com_mucheng_nodejava_core_Unlocker_nativeCreateUnlocker(JNIEnv *env, jobject thiz, | ||
jlong isolatePtr) { | ||
Isolate *isolate = Util::As<Isolate *>(isolatePtr); | ||
Unlocker *unlocker = new Unlocker(isolate); | ||
Unlocker::To(thiz, unlocker); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// | ||
// Created by 35785 on 2024/1/25. | ||
// | ||
|
||
#ifndef NODEJAVA_UNLOCKER_H | ||
#define NODEJAVA_UNLOCKER_H | ||
#include <jni.h> | ||
#include <v8.h> | ||
#include "Isolate.h" | ||
|
||
class Unlocker { | ||
public: | ||
Isolate *isolate; | ||
v8::Unlocker *self; | ||
|
||
Unlocker(Isolate *isolate); | ||
|
||
static void To(jobject instance, Unlocker *unlocker); | ||
|
||
static Unlocker *From(jobject instance); | ||
|
||
|
||
}; | ||
|
||
|
||
#endif //NODEJAVA_UNLOCKER_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
#include "embedding.h" | ||
#include "main.h" | ||
#include "log.h" | ||
#include "javabridge/ClassInfo.h" | ||
|
||
v8::Local<v8::Object> getClassInfo( | ||
v8::Isolate *isolate, | ||
v8::Local<v8::Context> context, | ||
v8::Local<v8::String> className | ||
) { | ||
JNIEnv *env = Main::env(); | ||
jclass javaBridgeUtilClass = env->FindClass("com/mucheng/nodejava/javabridge/JavaBridgeUtil"); | ||
jmethodID findClassOrNull = env->GetStaticMethodID(javaBridgeUtilClass, "findClassOrNull", | ||
"(Ljava/lang/String;)Lcom/mucheng/nodejava/javabridge/ClassInfo;"); | ||
jobject classInfoInstance = env->NewGlobalRef( | ||
env->CallStaticObjectMethod(javaBridgeUtilClass, findClassOrNull, | ||
env->NewStringUTF( | ||
*v8::String::Utf8Value(isolate, | ||
className)))); | ||
|
||
|
||
return ClassInfo::BuildObject(isolate, context, classInfoInstance); | ||
} | ||
|
||
void JAVA_ACCESSOR_BINDING( | ||
v8::Local<v8::Object> exports, | ||
v8::Local<v8::Value>, | ||
v8::Local<v8::Context> context, | ||
void *priv | ||
) { | ||
v8::Isolate *isolate = context->GetIsolate(); | ||
|
||
exports->Set( | ||
context, | ||
v8::String::NewFromUtf8Literal(isolate, "getClassInfo"), | ||
v8::Function::New(context, [](const v8::FunctionCallbackInfo<v8::Value> &info) { | ||
SETUP_CALLBACK_INFO(); | ||
info.GetReturnValue().Set(getClassInfo(isolate, context, info[0].As<v8::String>())); | ||
}).ToLocalChecked() | ||
).Check(); | ||
|
||
exports->Set( | ||
context, | ||
v8::String::NewFromUtf8Literal(isolate, "setUnsafeReflectionEnabled"), | ||
v8::Function::New(context, [](const v8::FunctionCallbackInfo<v8::Value> &info) { | ||
SETUP_CALLBACK_INFO(); | ||
JNIEnv *env = Main::env(); | ||
jclass javaBridgeUtilClass = env->FindClass( | ||
"com/mucheng/nodejava/javabridge/JavaBridgeUtil"); | ||
jfieldID unsafeReflectionEnabledField = env->GetStaticFieldID(javaBridgeUtilClass, | ||
"unsafeReflectionEnabled", | ||
"Z"); | ||
env->SetStaticBooleanField(javaBridgeUtilClass, unsafeReflectionEnabledField, | ||
info[0].As<v8::Boolean>()->Value()); | ||
}).ToLocalChecked() | ||
).Check(); | ||
|
||
exports->Set( | ||
context, | ||
v8::String::NewFromUtf8Literal(isolate, "isUnsafeReflectionEnabled"), | ||
v8::Function::New(context, [](const v8::FunctionCallbackInfo<v8::Value> &info) { | ||
SETUP_CALLBACK_INFO(); | ||
JNIEnv *env = Main::env(); | ||
jclass javaBridgeUtilClass = env->FindClass( | ||
"com/mucheng/nodejava/javabridge/JavaBridgeUtil"); | ||
jfieldID unsafeReflectionEnabledField = env->GetStaticFieldID(javaBridgeUtilClass, | ||
"unsafeReflectionEnabled", | ||
"Z"); | ||
info.GetReturnValue().Set(env->GetStaticBooleanField(javaBridgeUtilClass, | ||
unsafeReflectionEnabledField)); | ||
}).ToLocalChecked() | ||
).Check(); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#ifndef NODEJAVA_EMBEDDING_H | ||
#define NODEJAVA_EMBEDDING_H | ||
|
||
#include <node.h> | ||
#include <v8.h> | ||
|
||
#define SETUP_CALLBACK_INFO() v8::Isolate *isolate = info.GetIsolate(); \ | ||
v8::Local<v8::Context> context = isolate->GetCurrentContext(); | ||
|
||
void JAVA_ACCESSOR_BINDING( | ||
v8::Local<v8::Object> exports, | ||
v8::Local<v8::Value> module, | ||
v8::Local<v8::Context> context, | ||
void *priv | ||
); | ||
|
||
#endif //NODEJAVA_EMBEDDING_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.