Skip to content

Commit

Permalink
✨ feat: #351 Add V8ValueSealedArray
Browse files Browse the repository at this point in the history
  • Loading branch information
caoccao committed Jun 7, 2024
1 parent ea1283b commit 50d4787
Show file tree
Hide file tree
Showing 13 changed files with 457 additions and 294 deletions.
33 changes: 28 additions & 5 deletions cpp/jni/javet_converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@ namespace Javet {
jmethodID jmethodIDV8ValueRegExpConstructor;
jmethodID jmethodIDV8ValueRegExpGetHandle;

jclass jclassV8ValueSealedArray;
jmethodID jmethodIDV8ValueSealedArrayConstructor;
jmethodID jmethodIDV8ValueSealedArrayGetHandle;

jclass jclassV8ValueSet;
jmethodID jmethodIDV8ValueSetConstructor;
jmethodID jmethodIDV8ValueSetGetHandle;
Expand Down Expand Up @@ -478,6 +482,10 @@ 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 @@ -658,11 +666,26 @@ namespace Javet {
// Reference types
// Note: Reference types must be checked before primitive types are checked.
if (v8Value->IsArray()) {
return jniEnv->NewObject(
jclassV8ValueArray,
jmethodIDV8ValueArrayConstructor,
v8Runtime->externalV8Runtime,
ToV8PersistentReference(v8Context, v8Value));
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));
}
}
if (v8Value->IsTypedArray()) {
int type = V8ValueReferenceType::Invalid;
Expand Down
9 changes: 9 additions & 0 deletions cpp/jni/javet_converter.h
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,15 @@ namespace Javet {
#endif
}

static inline V8InternalJSObject ToV8InternalJSObject(
const V8LocalValue& v8LocalValue) noexcept {
#ifdef ENABLE_NODE
return V8InternalJSObject::cast(*v8::Utils::OpenHandle(*v8LocalValue));
#else
return *V8InternalJSObject::cast(*v8::Utils::OpenHandle(*v8LocalValue));
#endif
}

static inline V8InternalModule ToV8InternalModule(
const V8LocalModule& v8LocalModule) noexcept {
#ifdef ENABLE_NODE
Expand Down
1 change: 1 addition & 0 deletions cpp/jni/javet_v8_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ using V8InternalHeapObject = v8::internal::HeapObject;
using V8InternalIncrementalStringBuilder = v8::internal::IncrementalStringBuilder;
using V8InternalIsolate = v8::internal::Isolate;
using V8InternalJSFunction = v8::internal::JSFunction;
using V8InternalJSObject = v8::internal::JSObject;
using V8InternalModule = v8::internal::Module;
using V8InternalObject = v8::internal::Object;
using V8InternalRobustnessFlag = v8::internal::RobustnessFlag;
Expand Down
1 change: 1 addition & 0 deletions docs/release_notes/release_notes_3_1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Release Notes 3.1.x
* Added ``JavetProxyPrototypeStore``
* Added ``getPrototypeOf()`` to ``IJavetDirectProxyHandler`` and ``JavetDirectProxyObjectHandler``
* Added ``getGuard()`` to ``V8Runtime``
* Added ``V8ValueSealedArray``
* 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
8 changes: 4 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(IV8ValueArray iV8ValueArray) throws JavetException {
return v8Runtime.arrayGetLength(iV8ValueArray);
public int arrayGetLength(IV8ValueSealedArray iV8ValueSealedArray) throws JavetException {
return v8Runtime.arrayGetLength(iV8ValueSealedArray);
}

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

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

public int batchObjectGet(
Expand Down
18 changes: 9 additions & 9 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 iV8ValueArray the V8 value array
* @param iV8ValueSealedArray the V8 value sealed array
* @return the length
* @throws JavetException the javet exception
* @since 0.7.0
*/
@SuppressWarnings("RedundantThrows")
int arrayGetLength(IV8ValueArray iV8ValueArray) throws JavetException {
return v8Native.arrayGetLength(handle, iV8ValueArray.getHandle(), iV8ValueArray.getType().getId());
int arrayGetLength(IV8ValueSealedArray iV8ValueSealedArray) throws JavetException {
return v8Native.arrayGetLength(handle, iV8ValueSealedArray.getHandle(), iV8ValueSealedArray.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 iV8ValueArray the V8 value array
* @param v8Values the V8 values
* @param startIndex the start index
* @param endIndex the end index
* @param iV8ValueSealedArray the V8 value sealed 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(
IV8ValueArray iV8ValueArray, V8Value[] v8Values, int startIndex, int endIndex)
IV8ValueSealedArray iV8ValueSealedArray, V8Value[] v8Values, int startIndex, int endIndex)
throws JavetException {
return v8Native.batchArrayGet(
handle, iV8ValueArray.getHandle(), iV8ValueArray.getType().getId(),
handle, iV8ValueSealedArray.getHandle(), iV8ValueSealedArray.getType().getId(),
v8Values, startIndex, endIndex);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,11 @@ protected <T> T toObject(V8Value v8Value, final int depth) throws JavetException
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();
} else if (v8Value instanceof V8ValueSet) {
V8ValueSet v8ValueSet = (V8ValueSet) v8Value;
final HashSet<Object> set = new HashSet<>();
Expand Down
148 changes: 1 addition & 147 deletions src/main/java/com/caoccao/javet/values/reference/IV8ValueArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,7 @@
* @since 0.7.0
*/
@SuppressWarnings("unchecked")
public interface IV8ValueArray extends IV8ValueObject {
/**
* The constant FUNCTION_FLAT.
*
* @since 3.0.4
*/
String FUNCTION_FLAT = "flat";
public interface IV8ValueArray extends IV8ValueSealedArray {
/**
* The constant FUNCTION_SHIFT.
*
Expand All @@ -69,134 +63,6 @@ public interface IV8ValueArray extends IV8ValueObject {
*/
String FUNCTION_PUSH = "push";

/**
* Batch get the given range of items from the 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
*/
int batchGet(V8Value[] v8Values, int startIndex, int endIndex) throws JavetException;

/**
* Batch get all the items from the array.
*
* @param <T> the type parameter
* @return the V8 values
* @throws JavetException the javet exception
* @since 2.2.0
*/
default <T extends V8Value> T[] batchGet() throws JavetException {
final int length = getLength();
V8Value[] v8Values = new V8Value[length];
if (length > 0) {
try {
batchGet(v8Values, 0, length);
} catch (Throwable t) {
JavetResourceUtils.safeClose(v8Values);
Arrays.fill(v8Values, null);
throw t;
}
}
return (T[]) v8Values;
}

/**
* Array.prototype.flat().
* The flat() method of Array instances creates a new array with all sub-array elements concatenated
* into it recursively up to the default depth 1.
*
* @return the V8 value array
* @throws JavetException the javet exception
* @since 3.0.4
*/
@CheckReturnValue
default IV8ValueArray flat() throws JavetException {
return invoke(FUNCTION_FLAT);
}

/**
* Array.prototype.flat().
* The flat() method of Array instances creates a new array with all sub-array elements concatenated
* into it recursively up to the specified depth.
*
* @param depth the depth level specifying how deep a nested array structure should be flattened. Defaults to 1.
* @return the V8 value array
* @throws JavetException the javet exception
* @since 3.0.4
*/
@CheckReturnValue
default IV8ValueArray flat(int depth) throws JavetException {
return invoke(FUNCTION_FLAT, getV8Runtime().createV8ValueInteger(depth));
}

/**
* For each of the item, call the consumer and return the item count.
*
* @param <Value> the type parameter
* @param <E> the type parameter
* @param consumer the consumer
* @param batchSize the batch size
* @return the item count
* @throws JavetException the javet exception
* @throws E the custom exception
* @since 2.2.0
*/
<Value extends V8Value, E extends Throwable> int forEach(
IJavetUniConsumer<Value, E> consumer,
int batchSize)
throws JavetException, E;

/**
* For each of the item, call the consumer and return the item count.
*
* @param <Value> the type parameter
* @param <E> the type parameter
* @param consumer the consumer
* @param batchSize the batch size
* @return the item count
* @throws JavetException the javet exception
* @throws E the custom exception
* @since 2.2.0
*/
<Value extends V8Value, E extends Throwable> int forEach(
IJavetUniIndexedConsumer<Value, E> consumer,
int batchSize)
throws JavetException, E;

/**
* Get V8 value by index.
*
* @param <T> the type parameter
* @param index the index
* @return the V8 value
* @throws JavetException the javet exception
* @since 0.7.0
*/
@CheckReturnValue
<T extends V8Value> T get(int index) throws JavetException;

/**
* Gets keys.
*
* @return the keys
* @throws JavetException the javet exception
* @since 0.7.2
*/
List<Integer> getKeys() throws JavetException;

/**
* Gets length.
*
* @return the length
* @throws JavetException the javet exception
* @since 0.7.0
*/
int getLength() throws JavetException;

/**
* Array.prototype.pop().
* The pop() method of Array instances removes the last element from an array and returns that element.
Expand Down Expand Up @@ -535,18 +401,6 @@ default ZonedDateTime shiftZonedDateTime() throws JavetException {
return shiftPrimitive();
}

/**
* To V8 value array.
*
* @return the V8 value array
* @throws JavetException the javet exception
* @since 0.9.10
*/
@CheckReturnValue
default V8Value[] toArray() throws JavetException {
return batchGet();
}

/**
* Array.prototype.unshift().
* The unshift() method of Array instances adds the specified elements to the beginning of an array
Expand Down
Loading

0 comments on commit 50d4787

Please sign in to comment.