Skip to content

Commit

Permalink
✨ feat: #306 Throw TypeError if function is not found in invoke()
Browse files Browse the repository at this point in the history
  • Loading branch information
caoccao committed Mar 9, 2024
1 parent 70b67b0 commit 34567b2
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 11 deletions.
16 changes: 10 additions & 6 deletions cpp/jni/javet_jni_object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ JNIEXPORT jobject JNICALL Java_com_caoccao_javet_interop_V8Native_objectGet
[](JNIEnv* jniEnv, Javet::V8Runtime* v8Runtime, const V8LocalContext& v8Context, jbooleanArray mPrimitiveFlags) -> jobject {
return Javet::Converter::ToExternalV8ValueUndefined(jniEnv, v8Runtime);
},
[](JNIEnv* jniEnv, Javet::V8Runtime* v8Runtime, const V8LocalContext& v8Context) -> jobject { return nullptr; });
[](JNIEnv* jniEnv, Javet::V8Runtime* v8Runtime, const V8LocalContext& v8Context) -> jobject { return nullptr; });
}

JNIEXPORT jboolean JNICALL Java_com_caoccao_javet_interop_V8Native_objectGetBoolean
Expand All @@ -256,7 +256,7 @@ JNIEXPORT jboolean JNICALL Java_com_caoccao_javet_interop_V8Native_objectGetBool
jniEnv->SetBooleanArrayRegion(mPrimitiveFlags, 0, 1, Javet::V8ValueObject::defaultPrimitiveFlags);
return false;
},
[](JNIEnv* jniEnv, Javet::V8Runtime* v8Runtime, const V8LocalContext& v8Context) -> jboolean { return false; });
[](JNIEnv* jniEnv, Javet::V8Runtime* v8Runtime, const V8LocalContext& v8Context) -> jboolean { return false; });
}

JNIEXPORT jdouble JNICALL Java_com_caoccao_javet_interop_V8Native_objectGetDouble
Expand All @@ -281,7 +281,7 @@ JNIEXPORT jdouble JNICALL Java_com_caoccao_javet_interop_V8Native_objectGetDoubl
jniEnv->SetBooleanArrayRegion(mPrimitiveFlags, 0, 1, Javet::V8ValueObject::defaultPrimitiveFlags);
return 0;
},
[](JNIEnv* jniEnv, Javet::V8Runtime* v8Runtime, const V8LocalContext& v8Context) -> jdouble { return 0; });
[](JNIEnv* jniEnv, Javet::V8Runtime* v8Runtime, const V8LocalContext& v8Context) -> jdouble { return 0; });
}

JNIEXPORT jint JNICALL Java_com_caoccao_javet_interop_V8Native_objectGetIdentityHash
Expand Down Expand Up @@ -315,7 +315,7 @@ JNIEXPORT jint JNICALL Java_com_caoccao_javet_interop_V8Native_objectGetInteger
jniEnv->SetBooleanArrayRegion(mPrimitiveFlags, 0, 1, Javet::V8ValueObject::defaultPrimitiveFlags);
return 0;
},
[](JNIEnv* jniEnv, Javet::V8Runtime* v8Runtime, const V8LocalContext& v8Context) -> jint { return 0; });
[](JNIEnv* jniEnv, Javet::V8Runtime* v8Runtime, const V8LocalContext& v8Context) -> jint { return 0; });
}

JNIEXPORT jlong JNICALL Java_com_caoccao_javet_interop_V8Native_objectGetLong
Expand All @@ -340,7 +340,7 @@ JNIEXPORT jlong JNICALL Java_com_caoccao_javet_interop_V8Native_objectGetLong
jniEnv->SetBooleanArrayRegion(mPrimitiveFlags, 0, 1, Javet::V8ValueObject::defaultPrimitiveFlags);
return 0;
},
[](JNIEnv* jniEnv, Javet::V8Runtime* v8Runtime, const V8LocalContext& v8Context) -> jlong { return 0; });
[](JNIEnv* jniEnv, Javet::V8Runtime* v8Runtime, const V8LocalContext& v8Context) -> jlong { return 0; });
}

JNIEXPORT jobject JNICALL Java_com_caoccao_javet_interop_V8Native_objectGetPrivateProperty
Expand Down Expand Up @@ -501,7 +501,7 @@ JNIEXPORT jstring JNICALL Java_com_caoccao_javet_interop_V8Native_objectGetStrin
return nullptr;
},
[](JNIEnv* jniEnv, Javet::V8Runtime* v8Runtime, const V8LocalContext& v8Context, jbooleanArray mPrimitiveFlags) -> jstring { return nullptr; },
[](JNIEnv* jniEnv, Javet::V8Runtime* v8Runtime, const V8LocalContext& v8Context) -> jstring { return nullptr; });
[](JNIEnv* jniEnv, Javet::V8Runtime* v8Runtime, const V8LocalContext& v8Context) -> jstring { return nullptr; });
}

JNIEXPORT jboolean JNICALL Java_com_caoccao_javet_interop_V8Native_objectHas
Expand Down Expand Up @@ -608,6 +608,7 @@ JNIEXPORT jobject JNICALL Java_com_caoccao_javet_interop_V8Native_objectInvoke
if (Javet::Exceptions::HandlePendingException(jniEnv, v8Runtime, v8Context)) {
return nullptr;
}
return nullptr;
}
else {
auto v8Function = v8MaybeLocalValue.ToLocalChecked();
Expand All @@ -629,6 +630,9 @@ JNIEXPORT jobject JNICALL Java_com_caoccao_javet_interop_V8Native_objectInvoke
return v8Runtime->SafeToExternalV8Value(jniEnv, v8Context, v8MaybeLocalValueResult.ToLocalChecked());
}
}
else {
return nullptr;
}
}
}
return Javet::Converter::ToExternalV8ValueUndefined(jniEnv, v8Runtime);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.caoccao.javet.interfaces.IJavetEntityError;
import com.caoccao.javet.interop.converters.JavetObjectConverter;
import com.caoccao.javet.utils.StringUtils;
import com.caoccao.javet.values.V8Value;

import java.util.Map;
Expand Down Expand Up @@ -96,6 +97,27 @@ public final class JavetScriptingError {
this.startPosition = startPosition;
}

/**
* Instantiates a new Javet scripting error.
*
* @param message the message
* @param detailedMessage the detailed message
* @param stack the stack
* @since 3.1.0
*/
public JavetScriptingError(String message, String detailedMessage, String stack) {
this.detailedMessage = detailedMessage;
this.endColumn = 0;
this.endPosition = 0;
this.lineNumber = 0;
this.message = message;
this.resourceName = StringUtils.EMPTY;
this.sourceLine = StringUtils.EMPTY;
this.stack = stack;
this.startColumn = 0;
this.startPosition = 0;
}

/**
* Gets context.
*
Expand Down
13 changes: 12 additions & 1 deletion src/main/java/com/caoccao/javet/exceptions/V8ErrorTemplate.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,17 @@ public static String typeErrorReduceOfEmptyArrayWithNoInitialValue() {
return "Reduce of empty array with no initial value";
}

/**
* TypeError: ${value} is not a function.
*
* @param functionName the function name
* @return the message
* @since 3.1.0
*/
public static String typeErrorValueIsNotAFunction(String functionName) {
return Objects.requireNonNull(functionName) + " is not a function";
}

/**
* TypeError: ${value} is not a function.
*
Expand All @@ -81,6 +92,6 @@ public static String typeErrorReduceOfEmptyArrayWithNoInitialValue() {
* @since 3.0.4
*/
public static String typeErrorValueIsNotAFunction(V8Value v8Value) {
return V8ValueUtils.asString(v8Value) + " is not a function";
return typeErrorValueIsNotAFunction(V8ValueUtils.asString(v8Value));
}
}
20 changes: 16 additions & 4 deletions src/main/java/com/caoccao/javet/interop/V8Runtime.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@

import com.caoccao.javet.annotations.CheckReturnValue;
import com.caoccao.javet.enums.*;
import com.caoccao.javet.exceptions.JavetError;
import com.caoccao.javet.exceptions.JavetException;
import com.caoccao.javet.exceptions.*;
import com.caoccao.javet.interfaces.IEnumBitset;
import com.caoccao.javet.interfaces.IJavetClosable;
import com.caoccao.javet.interfaces.IJavetLogger;
Expand Down Expand Up @@ -2551,8 +2550,21 @@ boolean objectHasPrivateProperty(IV8ValueObject iV8ValueObject, String propertyN
<T extends V8Value> T objectInvoke(
IV8ValueObject iV8ValueObject, String functionName, boolean returnResult, V8Value... v8Values)
throws JavetException {
return (T) v8Native.objectInvoke(
handle, iV8ValueObject.getHandle(), iV8ValueObject.getType().getId(), functionName, returnResult, v8Values);
Object result = v8Native.objectInvoke(
handle,
iV8ValueObject.getHandle(),
iV8ValueObject.getType().getId(),
functionName,
returnResult,
v8Values);
if (result == null) {
String message = MessageFormat.format(
"{0}: {1}",
V8ValueErrorType.TypeError.getName(),
V8ErrorTemplate.typeErrorValueIsNotAFunction(functionName));
throw new JavetExecutionException(new JavetScriptingError(message, message, null), null);
}
return (T) result;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,11 @@ public void testInvokeObject() throws JavetException {
new Integer[]{1, 2, 3, 4, 5, 6},
result.toArray(new Integer[0]),
"invokeObject() should work transparently without resource leak");
assertEquals(
"TypeError: unknownFunction is not a function",
assertThrows(
JavetExecutionException.class,
() -> v8Runtime.getGlobalObject().invokeVoid("unknownFunction")).getMessage());
}

@Test
Expand All @@ -433,6 +438,11 @@ public void testInvokeVoid() throws JavetException {
assertEquals(4, v8ValueArray.getLength());
assertEquals(4, v8ValueArray.getInteger(3));
assertEquals("1,2,3,4", v8ValueArray.toString());
assertEquals(
"TypeError: unknownFunction is not a function",
assertThrows(
JavetExecutionException.class,
() -> v8ValueArray.invokeVoid("unknownFunction")).getMessage());
}
}

Expand Down

0 comments on commit 34567b2

Please sign in to comment.