Skip to content

Commit

Permalink
Javet v0.7.2 (#5)
Browse files Browse the repository at this point in the history
* Added ``setFunction(String functionName, String codeString)`` to ``IV8ValueObject``
* Added ``equals()`` and ``strictEquals()`` and ``sameValue()`` to ``IV8Value``
* Added ``getIdentityHash()`` to ``IV8ValueReference``
* Added ``isDead()``, ``isInUse()``, ``callAsConstructor()`` and ``terminateExecution()`` to ``V8Runtime``
* Added V8 typed array and data view
* Added ``IJavetEngineGuard``
  • Loading branch information
caoccao authored Mar 2, 2021
1 parent 3226163 commit 932ee83
Show file tree
Hide file tree
Showing 115 changed files with 4,037 additions and 638 deletions.
24 changes: 7 additions & 17 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,22 @@ Maven
<dependency>
<groupId>com.caoccao.javet</groupId>
<artifactId>javet</artifactId>
<version>0.7.1</version>
<version>0.7.2</version>
</dependency>
Gradle Kotlin DSL
^^^^^^^^^^^^^^^^^

.. code-block:: kotlin
implementation("com.caoccao.javet:javet:0.7.1")
implementation("com.caoccao.javet:javet:0.7.2")
Gradle Groovy DSL
^^^^^^^^^^^^^^^^^

.. code-block:: groovy
implementation 'com.caoccao.javet:javet:0.7.1'
implementation 'com.caoccao.javet:javet:0.7.2'
Hello Javet
-----------
Expand All @@ -62,25 +62,15 @@ Hello Javet
Documents
=========

* `Build <docs/build.rst>`_
* `Development <docs/development.rst>`_
* `Development <docs/development/index.rst>`_
* `Build <docs/build.rst>`_
* `Design <docs/design.rst>`_
* `Performance <docs/performance.rst>`_
* `Tutorial <docs/tutorial/index.rst>`_
* `Performance <docs/performance.rst>`_
* `Release Notes <docs/release_notes.rst>`_
* `FAQ <docs/faq/index.rst>`_
* `TODO List <docs/todo_list.rst>`_

Motivation
==========

I used to take a try of J2V8 and find it's quite compelling. However, J2V8 is slowly dying, with serious memory leak issues, V8 version issue, etc.

Sometimes starting from scratch implies lower cost than upgrading an existing solution. I think it might be true here in this project. I've learned quite a lot by manually fixing the Windows and Linux build system of J2V8.

Also, I had got many ideas on how the API will look like. At the end of 2020, I thought I would be able to write a new one from scratch and leave J2V8 behind. Indeed, I made it few months later.

Please refer to `History with J2V8 <docs/faq/history_with_j2v8.rst>`_ for detail.

License
=======

Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ repositories {
}

group = "com.caoccao.javet"
version = "0.7.1"
version = "0.7.2"

repositories {
mavenCentral()
Expand Down
2 changes: 1 addition & 1 deletion cpp/build.cmd
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@echo off
REM Usage sample: build -DV8_DIR=C:\v8
SET JAVET_VERSION=0.7.1
SET JAVET_VERSION=0.7.2
rd /s/q build
mkdir build
cd build
Expand Down
2 changes: 1 addition & 1 deletion cpp/build.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/sh

# Usage sample: build -DV8_DIR=~/v8
JAVET_VERSION=0.7.1
JAVET_VERSION=0.7.2
rm -rf build
mkdir build
cd build
Expand Down
92 changes: 89 additions & 3 deletions cpp/jni/com_caoccao_javet_interop_V8Native.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
#define TO_JAVA_INTEGER(jniEnv, obj) jniEnv->CallIntMethod(obj, Javet::Main::jmethodIDV8ValueIntegerToPrimitive)
#define TO_JAVA_STRING(jniEnv, obj) (jstring)jniEnv->CallObjectMethod(obj, Javet::Main::jmethodIDV8ValueStringToPrimitive)
#define IS_V8_ARRAY(type) (type == Javet::Enums::V8ValueReferenceType::Array)
#define IS_V8_ARRAY_BUFFER(type) (type == Javet::Enums::V8ValueReferenceType::ArrayBuffer)
#define IS_V8_ARGUMENTS(type) (type == Javet::Enums::V8ValueReferenceType::Arguments)
#define IS_V8_FUNCTION(type) (type == Javet::Enums::V8ValueReferenceType::Function)
#define IS_V8_MAP(type) (type == Javet::Enums::V8ValueReferenceType::Map)
Expand All @@ -69,7 +70,18 @@
v8::HandleScope v8HandleScope(v8Runtime->v8Isolate); \
auto v8Context = v8::Local<v8::Context>::New(v8Runtime->v8Isolate, v8Runtime->v8Context); \
v8::Context::Scope v8ContextScope(v8Context); \
auto v8LocalObject = v8PersistentObjectPointer->Get(v8Runtime->v8Isolate);
auto v8LocalObject = v8PersistentObjectPointer->Get(v8Context->GetIsolate());

#define RUNTIME_AND_2_VALUES_HANDLES_TO_OBJECTS_WITH_SCOPE(v8RuntimeHandle, v8ValueHandle1, v8ValueHandle2) \
auto v8Runtime = reinterpret_cast<Javet::V8Runtime*>(v8RuntimeHandle); \
auto v8PersistentObjectPointer1 = reinterpret_cast<v8::Persistent<v8::Object>*>(v8ValueHandle1); \
auto v8PersistentObjectPointer2 = reinterpret_cast<v8::Persistent<v8::Object>*>(v8ValueHandle2); \
v8::Isolate::Scope v8IsolateScope(v8Runtime->v8Isolate); \
v8::HandleScope v8HandleScope(v8Runtime->v8Isolate); \
auto v8Context = v8::Local<v8::Context>::New(v8Runtime->v8Isolate, v8Runtime->v8Context); \
v8::Context::Scope v8ContextScope(v8Context); \
auto v8LocalObject1 = v8PersistentObjectPointer1->Get(v8Context->GetIsolate()); \
auto v8LocalObject2 = v8PersistentObjectPointer2->Get(v8Context->GetIsolate());

#define SAFE_CONVERT_AND_RETURN_JAVE_V8_VALUE(jniEnv, v8Context, v8Value) \
try { \
Expand Down Expand Up @@ -182,6 +194,30 @@ JNIEXPORT jobject JNICALL Java_com_caoccao_javet_interop_V8Native_call
return nullptr;
}

JNIEXPORT jobject JNICALL Java_com_caoccao_javet_interop_V8Native_callAsConstructor
(JNIEnv* jniEnv, jclass callerClass, jlong v8RuntimeHandle, jlong v8ValueHandle, jint v8ValueType, jobjectArray mValues) {
RUNTIME_AND_VALUE_HANDLES_TO_OBJECTS_WITH_SCOPE(v8RuntimeHandle, v8ValueHandle);
if (v8LocalObject->IsFunction()) {
v8::TryCatch v8TryCatch(v8Runtime->v8Isolate);
v8::MaybeLocal<v8::Value> maybeLocalValueResult;
uint32_t valueCount = mValues == nullptr ? 0 : jniEnv->GetArrayLength(mValues);
if (valueCount > 0) {
auto umValuesPointer = Javet::Converter::ToV8Values(jniEnv, v8Context, mValues);
maybeLocalValueResult = v8LocalObject.As<v8::Function>()->CallAsConstructor(v8Context, valueCount, umValuesPointer.get());
}
else {
maybeLocalValueResult = v8LocalObject.As<v8::Function>()->CallAsConstructor(v8Context, 0, nullptr);
}
if (v8TryCatch.HasCaught()) {
Javet::Exceptions::ThrowJavetExecutionException(jniEnv, v8Context, v8TryCatch);
}
else {
SAFE_CONVERT_AND_RETURN_JAVE_V8_VALUE(jniEnv, v8Context, maybeLocalValueResult.ToLocalChecked());
}
}
return nullptr;
}

JNIEXPORT void JNICALL Java_com_caoccao_javet_interop_V8Native_clearWeak
(JNIEnv* jniEnv, jclass callerClass, jlong v8RuntimeHandle, jlong v8ValueHandle, jint v8ValueType) {
RUNTIME_AND_VALUE_HANDLES_TO_OBJECTS_WITH_SCOPE(v8RuntimeHandle, v8ValueHandle);
Expand Down Expand Up @@ -259,6 +295,11 @@ JNIEXPORT jobject JNICALL Java_com_caoccao_javet_interop_V8Native_createV8Value
else if (IS_V8_ARRAY(v8ValueType)) {
v8ValueValue = v8::Array::New(v8Context->GetIsolate());
}
else if (IS_V8_ARRAY_BUFFER(v8ValueType)) {
if (IS_JAVA_INTEGER(jniEnv, mContext)) {
v8ValueValue = v8::ArrayBuffer::New(v8Context->GetIsolate(), TO_JAVA_INTEGER(jniEnv, mContext));
}
}
else if (IS_V8_FUNCTION(v8ValueType)) {
jobject umContext = jniEnv->NewGlobalRef(mContext);
Javet::Callback::JavetCallbackContextReference javetCallbackContextReference(jniEnv, umContext);
Expand Down Expand Up @@ -309,6 +350,12 @@ JNIEXPORT jboolean JNICALL Java_com_caoccao_javet_interop_V8Native_delete
return false;
}

JNIEXPORT jboolean JNICALL Java_com_caoccao_javet_interop_V8Native_equals
(JNIEnv* jniEnv, jclass callerClass, jlong v8RuntimeHandle, jlong v8ValueHandle1, jlong v8ValueHandle2) {
RUNTIME_AND_2_VALUES_HANDLES_TO_OBJECTS_WITH_SCOPE(v8RuntimeHandle, v8ValueHandle1, v8ValueHandle2);
return v8LocalObject1->Equals(v8Context, v8LocalObject2).FromMaybe(false);
}

JNIEXPORT jobject JNICALL Java_com_caoccao_javet_interop_V8Native_execute
(JNIEnv* jniEnv, jclass callerClass, jlong v8RuntimeHandle, jstring mScript, jboolean mReturnResult,
jstring mResourceName, jint mResourceLineOffset, jint mResourceColumnOffset, jint mScriptId, jboolean mIsWASM, jboolean mIsModule) {
Expand Down Expand Up @@ -342,7 +389,7 @@ JNIEXPORT jobject JNICALL Java_com_caoccao_javet_interop_V8Native_get
RUNTIME_AND_VALUE_HANDLES_TO_OBJECTS_WITH_SCOPE(v8RuntimeHandle, v8ValueHandle);
auto v8ValueKey = Javet::Converter::ToV8Value(jniEnv, v8Context, key);
v8::Local<v8::Value> v8ValueValue;
if (IS_V8_ARRAY(v8ValueType) || IS_V8_ARGUMENTS(v8ValueType)) {
if (IS_V8_ARGUMENTS(v8ValueType) || IS_V8_ARRAY(v8ValueType) || v8LocalObject->IsTypedArray()) {
if (IS_JAVA_INTEGER(jniEnv, key)) {
jint integerKey = TO_JAVA_INTEGER(jniEnv, key);
if (integerKey >= 0) {
Expand Down Expand Up @@ -382,11 +429,20 @@ JNIEXPORT jobject JNICALL Java_com_caoccao_javet_interop_V8Native_getGlobalObjec
return Javet::Converter::ToExternalV8ValueGlobalObject(jniEnv, v8Runtime->v8GlobalObject);
}

JNIEXPORT jint JNICALL Java_com_caoccao_javet_interop_V8Native_getIdentityHash
(JNIEnv* jniEnv, jclass callerClass, jlong v8RuntimeHandle, jlong v8ValueHandle, jint v8ValueType) {
RUNTIME_AND_VALUE_HANDLES_TO_OBJECTS_WITH_SCOPE(v8RuntimeHandle, v8ValueHandle);
return v8LocalObject->GetIdentityHash();
}

JNIEXPORT jint JNICALL Java_com_caoccao_javet_interop_V8Native_getLength
(JNIEnv* jniEnv, jclass callerClass, jlong v8RuntimeHandle, jlong v8ValueHandle, jint v8ValueType) {
RUNTIME_AND_VALUE_HANDLES_TO_OBJECTS_WITH_SCOPE(v8RuntimeHandle, v8ValueHandle);
if (IS_V8_ARRAY(v8ValueType)) {
return v8LocalObject.As<v8::Array>()->Length();
return (jint)v8LocalObject.As<v8::Array>()->Length();
}
if (v8LocalObject->IsTypedArray()) {
return (jint)v8LocalObject.As<v8::TypedArray>()->Length();
}
return 0;
}
Expand Down Expand Up @@ -513,6 +569,18 @@ JNIEXPORT jobject JNICALL Java_com_caoccao_javet_interop_V8Native_invoke
return nullptr;
}

JNIEXPORT jboolean JNICALL Java_com_caoccao_javet_interop_V8Native_isDead
(JNIEnv* jniEnv, jclass callerClass, jlong v8RuntimeHandle) {
auto v8Runtime = reinterpret_cast<Javet::V8Runtime*>(v8RuntimeHandle);
return v8Runtime->v8Isolate->IsDead();
}

JNIEXPORT jboolean JNICALL Java_com_caoccao_javet_interop_V8Native_isInUse
(JNIEnv* jniEnv, jclass callerClass, jlong v8RuntimeHandle) {
auto v8Runtime = reinterpret_cast<Javet::V8Runtime*>(v8RuntimeHandle);
return v8Runtime->v8Isolate->IsInUse();
}

JNIEXPORT jboolean JNICALL Java_com_caoccao_javet_interop_V8Native_isWeak
(JNIEnv* jniEnv, jclass callerClass, jlong v8RuntimeHandle, jlong v8ValueHandle, jint v8ValueType) {
RUNTIME_AND_VALUE_HANDLES_TO_OBJECTS_WITH_SCOPE(v8RuntimeHandle, v8ValueHandle);
Expand Down Expand Up @@ -670,6 +738,24 @@ JNIEXPORT void JNICALL Java_com_caoccao_javet_interop_V8Native_setWeak
}
}

JNIEXPORT jboolean JNICALL Java_com_caoccao_javet_interop_V8Native_sameValue
(JNIEnv* jniEnv, jclass callerClass, jlong v8RuntimeHandle, jlong v8ValueHandle1, jlong v8ValueHandle2) {
RUNTIME_AND_2_VALUES_HANDLES_TO_OBJECTS_WITH_SCOPE(v8RuntimeHandle, v8ValueHandle1, v8ValueHandle2);
return v8LocalObject1->SameValue(v8LocalObject2);
}

JNIEXPORT jboolean JNICALL Java_com_caoccao_javet_interop_V8Native_strictEquals
(JNIEnv* jniEnv, jclass callerClass, jlong v8RuntimeHandle, jlong v8ValueHandle1, jlong v8ValueHandle2) {
RUNTIME_AND_2_VALUES_HANDLES_TO_OBJECTS_WITH_SCOPE(v8RuntimeHandle, v8ValueHandle1, v8ValueHandle2);
return v8LocalObject1->StrictEquals(v8LocalObject2);
}

JNIEXPORT void JNICALL Java_com_caoccao_javet_interop_V8Native_terminateExecution
(JNIEnv* jniEnv, jclass callerClass, jlong v8RuntimeHandle) {
auto v8Runtime = reinterpret_cast<Javet::V8Runtime*>(v8RuntimeHandle);
v8Runtime->v8Isolate->TerminateExecution();
}

JNIEXPORT jstring JNICALL Java_com_caoccao_javet_interop_V8Native_toProtoString
(JNIEnv* jniEnv, jclass callerClass, jlong v8RuntimeHandle, jlong v8ValueHandle, jint v8ValueType) {
RUNTIME_AND_VALUE_HANDLES_TO_OBJECTS_WITH_SCOPE(v8RuntimeHandle, v8ValueHandle);
Expand Down
64 changes: 64 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.

12 changes: 6 additions & 6 deletions cpp/jni/javet.rc
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US
//

VS_VERSION_INFO VERSIONINFO
FILEVERSION 0,7,1,0
PRODUCTVERSION 0,7,1,0
FILEVERSION 0,7,2,0
PRODUCTVERSION 0,7,2,0
FILEFLAGSMASK 0x3fL
#ifdef _DEBUG
FILEFLAGS 0x1L
Expand All @@ -79,12 +79,12 @@ BEGIN
BEGIN
VALUE "CompanyName", "caoccao.com"
VALUE "FileDescription", "caoccao.com"
VALUE "FileVersion", "0.7.1.0"
VALUE "InternalName", "javet-windows-x86_64.v.0.7.1.dll"
VALUE "FileVersion", "0.7.2.0"
VALUE "InternalName", "javet-windows-x86_64.v.0.7.2.dll"
VALUE "LegalCopyright", "Copyright (C) 2021"
VALUE "OriginalFilename", "javet-windows-x86_64.v.0.7.1.dll"
VALUE "OriginalFilename", "javet-windows-x86_64.v.0.7.2.dll"
VALUE "ProductName", "Javet Windows"
VALUE "ProductVersion", "0.7.1.0"
VALUE "ProductVersion", "0.7.2.0"
END
END
BLOCK "VarFileInfo"
Expand Down
Loading

0 comments on commit 932ee83

Please sign in to comment.