Skip to content

Commit

Permalink
✨ feat: #351 Add isSealed to V8ValueObject
Browse files Browse the repository at this point in the history
  • Loading branch information
caoccao committed Jun 7, 2024
1 parent 50d4787 commit 3844628
Show file tree
Hide file tree
Showing 18 changed files with 383 additions and 449 deletions.
8 changes: 8 additions & 0 deletions cpp/jni/com_caoccao_javet_interop_V8Native.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 5 additions & 28 deletions cpp/jni/javet_converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,6 @@ namespace Javet {
jmethodID jmethodIDV8ValueRegExpConstructor;
jmethodID jmethodIDV8ValueRegExpGetHandle;

jclass jclassV8ValueSealedArray;
jmethodID jmethodIDV8ValueSealedArrayConstructor;
jmethodID jmethodIDV8ValueSealedArrayGetHandle;

jclass jclassV8ValueSet;
jmethodID jmethodIDV8ValueSetConstructor;
jmethodID jmethodIDV8ValueSetGetHandle;
Expand Down Expand Up @@ -482,10 +478,6 @@ namespace Javet {
jmethodIDV8ValueRegExpConstructor = GET_METHOD_CONSTRUCTOR(jniEnv, jclassV8ValueRegExp);
jmethodIDV8ValueRegExpGetHandle = GET_METHOD_GET_HANDLE(jniEnv, jclassV8ValueRegExp);

jclassV8ValueSealedArray = FIND_CLASS(jniEnv, "com/caoccao/javet/values/reference/V8ValueSealedArray");
jmethodIDV8ValueSealedArrayConstructor = GET_METHOD_CONSTRUCTOR(jniEnv, jclassV8ValueSealedArray);
jmethodIDV8ValueSealedArrayGetHandle = GET_METHOD_GET_HANDLE(jniEnv, jclassV8ValueSealedArray);

jclassV8ValueSet = FIND_CLASS(jniEnv, "com/caoccao/javet/values/reference/V8ValueSet");
jmethodIDV8ValueSetConstructor = GET_METHOD_CONSTRUCTOR(jniEnv, jclassV8ValueSet);
jmethodIDV8ValueSetGetHandle = GET_METHOD_GET_HANDLE(jniEnv, jclassV8ValueSet);
Expand Down Expand Up @@ -666,26 +658,11 @@ namespace Javet {
// Reference types
// Note: Reference types must be checked before primitive types are checked.
if (v8Value->IsArray()) {
auto v8InternalJSObject = ToV8InternalJSObject(v8Value);
#ifdef ENABLE_NODE
auto elementKind = V8InternalJSObject::cast(v8InternalJSObject).GetElementsKind();
#else
auto elementKind = V8InternalJSObject::cast(v8InternalJSObject)->GetElementsKind();
#endif
if (v8::internal::IsSealedElementsKind(elementKind)) {
return jniEnv->NewObject(
jclassV8ValueSealedArray,
jmethodIDV8ValueSealedArrayConstructor,
v8Runtime->externalV8Runtime,
ToV8PersistentReference(v8Context, v8Value));
}
else {
return jniEnv->NewObject(
jclassV8ValueArray,
jmethodIDV8ValueArrayConstructor,
v8Runtime->externalV8Runtime,
ToV8PersistentReference(v8Context, v8Value));
}
return jniEnv->NewObject(
jclassV8ValueArray,
jmethodIDV8ValueArrayConstructor,
v8Runtime->externalV8Runtime,
ToV8PersistentReference(v8Context, v8Value));
}
if (v8Value->IsTypedArray()) {
int type = V8ValueReferenceType::Invalid;
Expand Down
15 changes: 15 additions & 0 deletions cpp/jni/javet_jni_object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,21 @@ JNIEXPORT jobject JNICALL Java_com_caoccao_javet_interop_V8Native_objectInvoke
return Javet::Converter::ToExternalV8ValueUndefined(jniEnv, v8Runtime);
}

JNIEXPORT jboolean JNICALL Java_com_caoccao_javet_interop_V8Native_objectIsSealed
(JNIEnv* jniEnv, jobject caller, jlong v8RuntimeHandle, jlong v8ValueHandle) {
RUNTIME_AND_VALUE_HANDLES_TO_OBJECTS_WITH_SCOPE(v8RuntimeHandle, v8ValueHandle);
if (v8LocalValue->IsObject()) {
auto v8InternalJSObject = Javet::Converter::ToV8InternalJSObject(v8LocalValue);
#ifdef ENABLE_NODE
auto elementKind = V8InternalJSObject::cast(v8InternalJSObject).GetElementsKind();
#else
auto elementKind = V8InternalJSObject::cast(v8InternalJSObject)->GetElementsKind();
#endif
return v8::internal::IsSealedElementsKind(elementKind);
}
return false;
}

JNIEXPORT jboolean JNICALL Java_com_caoccao_javet_interop_V8Native_objectSet
(JNIEnv* jniEnv, jobject caller, jlong v8RuntimeHandle, jlong v8ValueHandle, jint v8ValueType, jobjectArray keysAndValues) {
RUNTIME_AND_VALUE_HANDLES_TO_OBJECTS_WITH_SCOPE(v8RuntimeHandle, v8ValueHandle);
Expand Down
3 changes: 2 additions & 1 deletion docs/release_notes/release_notes_3_1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ Release Notes 3.1.x
* Added ``JavetProxyPrototypeStore``
* Added ``getPrototypeOf()`` to ``IJavetDirectProxyHandler`` and ``JavetDirectProxyObjectHandler``
* Added ``getGuard()`` to ``V8Runtime``
* Added ``V8ValueSealedArray``
* Added ``isSealed`` to ``V8ValueObject``
* Updated ``JavetObjectConverter`` to convert sealed array to ``Object[]`` instead of ``List<Object>``
* Replaced ``JavetEngineGuard`` with ``V8Guard``
* Removed ``executorService``, ``engineGuardCheckIntervalMillis`` from ``JavetEngineConfig``
* Patched V8 `Check failed: !IsFreeSpaceOrFillerMap(map) <https://groups.google.com/g/v8-dev/c/TCGnZKjYFEI/m/uDOciJsHAQAJ>`_
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/caoccao/javet/interop/IV8Native.java
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,8 @@ Object objectInvoke(
long v8RuntimeHandle, long v8ValueHandle, int v8ValueType,
String functionName, boolean returnResult, Object[] values);

boolean objectIsSealed(long v8RuntimeHandle, long v8ValueHandle);

boolean objectSet(long v8RuntimeHandle, long v8ValueHandle, int v8ValueType, Object[] keysAndValues);

boolean objectSetAccessor(
Expand Down
12 changes: 8 additions & 4 deletions src/main/java/com/caoccao/javet/interop/V8Internal.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,18 @@ public void addReference(IV8ValueReference iV8ValueReference) {
v8Runtime.addReference(iV8ValueReference);
}

public int arrayGetLength(IV8ValueSealedArray iV8ValueSealedArray) throws JavetException {
return v8Runtime.arrayGetLength(iV8ValueSealedArray);
public int arrayGetLength(IV8ValueArray iV8ValueArray) throws JavetException {
return v8Runtime.arrayGetLength(iV8ValueArray);
}

public int arrayGetLength(IV8ValueTypedArray iV8ValueTypedArray) throws JavetException {
return v8Runtime.arrayGetLength(iV8ValueTypedArray);
}

public int batchArrayGet(
IV8ValueSealedArray iV8ValueSealedArray, V8Value[] v8Values, int startIndex, int endIndex)
IV8ValueArray iV8ValueArray, V8Value[] v8Values, int startIndex, int endIndex)
throws JavetException {
return v8Runtime.batchArrayGet(iV8ValueSealedArray, v8Values, startIndex, endIndex);
return v8Runtime.batchArrayGet(iV8ValueArray, v8Values, startIndex, endIndex);
}

public int batchObjectGet(
Expand Down Expand Up @@ -449,6 +449,10 @@ public <T extends V8Value> T objectInvoke(
return v8Runtime.objectInvoke(iV8ValueObject, functionName, returnResult, v8Values);
}

public boolean objectIsSealed(IV8ValueObject iV8ValueObject) {
return v8Runtime.objectIsSealed(iV8ValueObject);
}

public boolean objectSet(IV8ValueObject iV8ValueObject, V8Value... v8Values) throws JavetException {
return v8Runtime.objectSet(iV8ValueObject, v8Values);
}
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/com/caoccao/javet/interop/V8Native.java
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,9 @@ public native Object objectInvoke(
long v8RuntimeHandle, long v8ValueHandle, int v8ValueType,
String functionName, boolean returnResult, Object[] values);

@Override
public native boolean objectIsSealed(long v8RuntimeHandle, long v8ValueHandle) ;

@Override
public native boolean objectSet(
long v8RuntimeHandle, long v8ValueHandle, int v8ValueType, Object[] keysAndValues);
Expand Down
35 changes: 25 additions & 10 deletions src/main/java/com/caoccao/javet/interop/V8Runtime.java
Original file line number Diff line number Diff line change
Expand Up @@ -446,14 +446,14 @@ public void allowEval(boolean allow) {
/**
* Gets length from an array.
*
* @param iV8ValueSealedArray the V8 value sealed array
* @param iV8ValueArray the V8 value array
* @return the length
* @throws JavetException the javet exception
* @since 0.7.0
*/
@SuppressWarnings("RedundantThrows")
int arrayGetLength(IV8ValueSealedArray iV8ValueSealedArray) throws JavetException {
return v8Native.arrayGetLength(handle, iV8ValueSealedArray.getHandle(), iV8ValueSealedArray.getType().getId());
int arrayGetLength(IV8ValueArray iV8ValueArray) throws JavetException {
return v8Native.arrayGetLength(handle, iV8ValueArray.getHandle(), iV8ValueArray.getType().getId());
}

/**
Expand Down Expand Up @@ -496,20 +496,20 @@ public boolean await(V8AwaitMode v8AwaitMode) {
/**
* Get the given range of items from the array.
*
* @param iV8ValueSealedArray the V8 value sealed array
* @param v8Values the V8 values
* @param startIndex the start index
* @param endIndex the end index
* @param iV8ValueArray the V8 value array
* @param v8Values the V8 values
* @param startIndex the start index
* @param endIndex the end index
* @return the actual item count
* @throws JavetException the javet exception
* @since 2.2.0
*/
@SuppressWarnings("RedundantThrows")
int batchArrayGet(
IV8ValueSealedArray iV8ValueSealedArray, V8Value[] v8Values, int startIndex, int endIndex)
IV8ValueArray iV8ValueArray, V8Value[] v8Values, int startIndex, int endIndex)
throws JavetException {
return v8Native.batchArrayGet(
handle, iV8ValueSealedArray.getHandle(), iV8ValueSealedArray.getType().getId(),
handle, iV8ValueArray.getHandle(), iV8ValueArray.getType().getId(),
v8Values, startIndex, endIndex);
}

Expand Down Expand Up @@ -2654,6 +2654,17 @@ <T extends V8Value> T objectInvoke(
return (T) result;
}

/**
* Object is sealed.
*
* @param iV8ValueObject the V8 value object
* @return true : yes, false: no
* @since 3.1.3
*/
public boolean objectIsSealed(IV8ValueObject iV8ValueObject) {
return v8Native.objectIsSealed(handle, Objects.requireNonNull(iV8ValueObject).getHandle());
}

/**
* Sets a property of an object by a key
*
Expand All @@ -2666,7 +2677,11 @@ <T extends V8Value> T objectInvoke(
@SuppressWarnings("RedundantThrows")
boolean objectSet(IV8ValueObject iV8ValueObject, V8Value... v8Values) throws JavetException {
assert v8Values.length > 0 && v8Values.length % 2 == 0 : ERROR_THE_KEY_VALUE_PAIR_MUST_MATCH;
return v8Native.objectSet(handle, iV8ValueObject.getHandle(), iV8ValueObject.getType().getId(), v8Values);
return v8Native.objectSet(
handle,
Objects.requireNonNull(iV8ValueObject).getHandle(),
iV8ValueObject.getType().getId(),
v8Values);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,12 +208,7 @@ protected <T> T toObject(V8Value v8Value, final int depth) throws JavetException
V8ValueArray v8ValueArray = (V8ValueArray) v8Value;
final List<Object> list = new ArrayList<>();
v8ValueArray.forEach(value -> list.add(toObject(value, depth + 1)));
return (T) list;
} else if (v8Value instanceof V8ValueSealedArray) {
V8ValueSealedArray v8ValueSealedArray = (V8ValueSealedArray) v8Value;
final List<Object> list = new ArrayList<>();
v8ValueSealedArray.forEach(value -> list.add(toObject(value, depth + 1)));
return (T) list.toArray();
return (T) (v8ValueArray.isSealed() ? list.toArray() : list);
} else if (v8Value instanceof V8ValueSet) {
V8ValueSet v8ValueSet = (V8ValueSet) v8Value;
final HashSet<Object> set = new HashSet<>();
Expand Down
Loading

0 comments on commit 3844628

Please sign in to comment.