Skip to content

Commit bcc2d5d

Browse files
committed
Implementation of Isolate::RequestInterrupt().
1 parent 37add07 commit bcc2d5d

File tree

8 files changed

+44
-2
lines changed

8 files changed

+44
-2
lines changed

graal-nodejs/deps/v8/src/graal/callbacks.cc

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,8 @@ static const JNINativeMethod callbacks[] = {
120120
CALLBACK("throwDataCloneError", "(JLjava/lang/Object;)V", &GraalThrowDataCloneError),
121121
CALLBACK("getSharedArrayBufferId", "(JLjava/lang/Object;)I", &GraalGetSharedArrayBufferId),
122122
CALLBACK("getSharedArrayBufferFromId", "(JI)Ljava/lang/Object;", &GraalGetSharedArrayBufferFromId),
123-
CALLBACK("syntheticModuleEvaluationSteps", "(JLjava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;", &GraalSyntheticModuleEvaluationSteps)
123+
CALLBACK("syntheticModuleEvaluationSteps", "(JLjava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;", &GraalSyntheticModuleEvaluationSteps),
124+
CALLBACK("executeInterruptCallback", "(JJ)V", &GraalExecuteInterruptCallback),
124125
};
125126

126127
static const int CALLBACK_COUNT = sizeof(callbacks) / sizeof(*callbacks);
@@ -853,3 +854,9 @@ jobject GraalSyntheticModuleEvaluationSteps(JNIEnv* env, jclass nativeAccess, jl
853854
return env->NewLocalRef(graal_value->GetJavaObject());
854855
}
855856
}
857+
858+
void GraalExecuteInterruptCallback(JNIEnv* env, jclass nativeAccess, jlong callback, jlong data) {
859+
GraalIsolate* graal_isolate = CurrentIsolateChecked();
860+
v8::Isolate* isolate = reinterpret_cast<v8::Isolate*> (graal_isolate);
861+
((v8::InterruptCallback) callback)(isolate, (void*) data);
862+
}

graal-nodejs/deps/v8/src/graal/callbacks.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,4 +156,6 @@ jobject GraalExecutePrepareStackTraceCallback(JNIEnv* env, jclass nativeAccess,
156156

157157
jobject GraalSyntheticModuleEvaluationSteps(JNIEnv* env, jclass nativeAccess, jlong callback, jobject java_context, jobject java_module);
158158

159+
void GraalExecuteInterruptCallback(JNIEnv* env, jclass nativeAccess, jlong callback, jlong data);
160+
159161
#endif /* CALLBACKS_H_ */

graal-nodejs/deps/v8/src/graal/graal_isolate.cc

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -724,6 +724,7 @@ GraalIsolate::GraalIsolate(JavaVM* jvm, JNIEnv* env, v8::Isolate::CreateParams c
724724
ACCESS_METHOD(GraalAccessMethod::isolate_terminate_execution, "isolateTerminateExecution", "()V")
725725
ACCESS_METHOD(GraalAccessMethod::isolate_cancel_terminate_execution, "isolateCancelTerminateExecution", "()V")
726726
ACCESS_METHOD(GraalAccessMethod::isolate_is_execution_terminating, "isolateIsExecutionTerminating", "()Z")
727+
ACCESS_METHOD(GraalAccessMethod::isolate_request_interrupt, "isolateRequestInterrupt", "(JJ)V")
727728
ACCESS_METHOD(GraalAccessMethod::isolate_get_int_placeholder, "isolateGetIntPlaceholder", "()Ljava/lang/Object;")
728729
ACCESS_METHOD(GraalAccessMethod::isolate_get_safe_int_placeholder, "isolateGetSafeIntPlaceholder", "()Ljava/lang/Object;")
729730
ACCESS_METHOD(GraalAccessMethod::isolate_get_double_placeholder, "isolateGetDoublePlaceholder", "()Ljava/lang/Object;")
@@ -1499,3 +1500,21 @@ v8::ArrayBuffer::Allocator* GraalIsolate::GetArrayBufferAllocator() {
14991500
void GraalIsolate::SchedulePauseOnNextStatement() {
15001501
JNI_CALL_VOID(this, GraalAccessMethod::isolate_schedule_pause_on_next_statement);
15011502
}
1503+
1504+
void GraalIsolate::RequestInterrupt(v8::InterruptCallback callback, void* data) {
1505+
// We cannot use GetJNIEnv()/JNI_CALL_VOID because RequestInterrupt()
1506+
// can be called from a thread that does not correspond to this isolate
1507+
GraalIsolate* current_isolate = CurrentIsolate();
1508+
JNIEnv* env;
1509+
if (current_isolate == nullptr) {
1510+
// the thread does not belong to any isolate (i.e. is not attached to JVM)
1511+
jvm_->AttachCurrentThread((void**) &env, nullptr);
1512+
} else {
1513+
env = current_isolate->GetJNIEnv();
1514+
}
1515+
jmethodID method_id = GetJNIMethod(GraalAccessMethod::isolate_request_interrupt);
1516+
env->functions->CallVoidMethod(env, access_, method_id, (jlong) callback, (jlong) data);
1517+
if (current_isolate == nullptr) {
1518+
jvm_->DetachCurrentThread();
1519+
}
1520+
}

graal-nodejs/deps/v8/src/graal/graal_isolate.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ enum GraalAccessMethod {
206206
isolate_terminate_execution,
207207
isolate_cancel_terminate_execution,
208208
isolate_is_execution_terminating,
209+
isolate_request_interrupt,
209210
isolate_get_int_placeholder,
210211
isolate_get_safe_int_placeholder,
211212
isolate_get_double_placeholder,
@@ -475,6 +476,7 @@ class GraalIsolate {
475476
void TerminateExecution();
476477
void CancelTerminateExecution();
477478
bool IsExecutionTerminating();
479+
void RequestInterrupt(v8::InterruptCallback callback, void* data);
478480

479481
inline JNIEnv* GetJNIEnv() {
480482
return jni_env_;

graal-nodejs/deps/v8/src/graal/v8.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3523,7 +3523,7 @@ namespace v8 {
35233523
}
35243524

35253525
void Isolate::RequestInterrupt(InterruptCallback callback, void* data) {
3526-
TRACE
3526+
reinterpret_cast<GraalIsolate*> (this)->RequestInterrupt(callback, data);
35273527
}
35283528

35293529
void Isolate::ClearKeptObjects() {

graal-nodejs/mx.graal-nodejs/com.oracle.truffle.trufflenode/src/com/oracle/truffle/trufflenode/GraalJSAccess.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3215,6 +3215,14 @@ public synchronized void isolateTerminateExecution() {
32153215
}
32163216
}
32173217

3218+
public void isolateRequestInterrupt(long callback, long data) {
3219+
Debugger debugger = lookupInstrument("debugger", Debugger.class);
3220+
debugger.startSession(se -> {
3221+
se.getSession().close();
3222+
NativeAccess.executeInterruptCallback(callback, data);
3223+
}).suspendNextExecution();
3224+
}
3225+
32183226
static final class GraalJSKillException extends ThreadDeath {
32193227
private static final long serialVersionUID = 3930431622452607906L;
32203228
}

graal-nodejs/mx.graal-nodejs/com.oracle.truffle.trufflenode/src/com/oracle/truffle/trufflenode/NativeAccess.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,4 +164,6 @@ public static native Object executePropertyHandlerDefiner(long functionPointer,
164164

165165
public static native Object syntheticModuleEvaluationSteps(long callback, Object context, Object module);
166166

167+
public static native void executeInterruptCallback(long callback, long data);
168+
167169
}

graal-nodejs/mx.graal-nodejs/com.oracle.truffle.trufflenode/src/svmnodejs.jniconfig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@
108108
{ "name": "isolateIsExecutionTerminating" },
109109
{ "name": "isolateMeasureMemory" },
110110
{ "name": "isolatePerformGC" },
111+
{ "name": "isolateRequestInterrupt" },
111112
{ "name": "isolateRunMicrotasks" },
112113
{ "name": "isolateSchedulePauseOnNextStatement" },
113114
{ "name": "isolateTerminateExecution" },
@@ -326,6 +327,7 @@
326327
{ "name": "executeFunction4" },
327328
{ "name": "executeFunction5" },
328329
{ "name": "executeFunction6" },
330+
{ "name": "executeInterruptCallback" },
329331
{ "name": "executePrepareStackTraceCallback" },
330332
{ "name": "executePropertyHandlerDefiner" },
331333
{ "name": "executePropertyHandlerDeleter" },

0 commit comments

Comments
 (0)