Skip to content

Commit ad3c6de

Browse files
committed
Add library
1 parent 88ce10c commit ad3c6de

File tree

11 files changed

+358
-2
lines changed

11 files changed

+358
-2
lines changed

library/CMakeLists.txt

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
cmake_minimum_required(VERSION 3.4.1)
2+
3+
project(quickjs-android)
4+
5+
add_subdirectory(../quickjs ${CMAKE_CURRENT_BINARY_DIR}/quickjs)
6+
7+
set(QUICKJS_ANDROID_SOURCES
8+
src/main/c/quickjs-jni.c
9+
)
10+
11+
add_library(quickjs-android SHARED ${QUICKJS_ANDROID_SOURCES})
12+
target_link_libraries(quickjs-android PRIVATE quickjs)

library/build.gradle

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2019 Hippo Seven
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
apply plugin: 'com.android.library'
18+
19+
android {
20+
compileSdkVersion 28
21+
22+
defaultConfig {
23+
minSdkVersion 17
24+
targetSdkVersion 28
25+
versionCode 1
26+
versionName '1.0'
27+
testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
28+
externalNativeBuild {
29+
cmake {
30+
targets 'quickjs-android'
31+
arguments '-DCMAKE_VERBOSE_MAKEFILE:BOOL=ON'
32+
}
33+
}
34+
}
35+
36+
buildTypes {
37+
release {
38+
minifyEnabled false
39+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
40+
}
41+
}
42+
43+
externalNativeBuild {
44+
cmake {
45+
path 'CMakeLists.txt'
46+
}
47+
}
48+
}
49+
50+
dependencies {
51+
androidTestImplementation 'androidx.test:core:1.2.0'
52+
androidTestImplementation 'androidx.test:runner:1.2.0'
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2019 Hippo Seven
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.hippo.quickjs.android.test;
18+
19+
import androidx.test.runner.AndroidJUnit4;
20+
import com.hippo.quickjs.android.JSContext;
21+
import com.hippo.quickjs.android.JSRuntime;
22+
import org.junit.Test;
23+
import org.junit.runner.RunWith;
24+
25+
import static org.junit.Assert.assertEquals;
26+
27+
@RunWith(AndroidJUnit4.class)
28+
public class JSContextTest {
29+
30+
@Test
31+
public void test() {
32+
try (JSRuntime runtime = JSRuntime.create()) {
33+
try (JSContext context = runtime.createContext()) {
34+
assertEquals(null, context.evaluate("1", "unknown.js", 0));
35+
}
36+
}
37+
}
38+
}

library/src/main/AndroidManifest.xml

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<!--
2+
~ Copyright 2019 Hippo Seven
3+
~
4+
~ Licensed under the Apache License, Version 2.0 (the "License");
5+
~ you may not use this file except in compliance with the License.
6+
~ You may obtain a copy of the License at
7+
~
8+
~ http://www.apache.org/licenses/LICENSE-2.0
9+
~
10+
~ Unless required by applicable law or agreed to in writing, software
11+
~ distributed under the License is distributed on an "AS IS" BASIS,
12+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
~ See the License for the specific language governing permissions and
14+
~ limitations under the License.
15+
-->
16+
17+
<manifest package="com.hippo.quickjs.android"/>

library/src/main/c/quickjs-jni.c

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#include <jni.h>
2+
#include <quickjs.h>
3+
4+
JNIEXPORT jlong JNICALL
5+
Java_com_hippo_quickjs_android_QuickJS_createRuntime(JNIEnv *env, jclass clazz) {
6+
return (jlong) JS_NewRuntime();
7+
}
8+
9+
JNIEXPORT void JNICALL
10+
Java_com_hippo_quickjs_android_QuickJS_destroyRuntime(JNIEnv *env, jclass clazz, jlong runtime) {
11+
JSRuntime *rt = (JSRuntime *) runtime;
12+
if (rt != NULL) {
13+
JS_FreeRuntime(rt);
14+
}
15+
}
16+
17+
JNIEXPORT jlong JNICALL
18+
Java_com_hippo_quickjs_android_QuickJS_createContext(JNIEnv *env, jclass clazz, jlong runtime) {
19+
JSRuntime *rt = (JSRuntime *) runtime;
20+
if (rt == NULL) {
21+
return 0;
22+
}
23+
return (jlong) JS_NewContext(rt);
24+
}
25+
26+
JNIEXPORT void JNICALL
27+
Java_com_hippo_quickjs_android_QuickJS_destroyContext(JNIEnv *env, jclass clazz, jlong context) {
28+
JSContext *ctx = (JSContext *) context;
29+
if (ctx != NULL) {
30+
JS_FreeContext(ctx);
31+
}
32+
}
33+
34+
JNIEXPORT jobject JNICALL
35+
Java_com_hippo_quickjs_android_QuickJS_evaluate(JNIEnv *env, jclass clazz,
36+
jlong context, jstring source_code, jstring file_name, jint flags) {
37+
JSContext *ctx = (JSContext *) context;
38+
if (ctx == NULL || source_code == NULL || file_name == NULL) {
39+
return 0;
40+
}
41+
42+
const char *source_code_utf = NULL;
43+
jsize source_code_length = 0;
44+
const char *file_name_utf = NULL;
45+
jobject result = NULL;
46+
47+
source_code_utf = (*env)->GetStringUTFChars(env, source_code, NULL);
48+
source_code_length = (*env)->GetStringUTFLength(env, source_code);
49+
file_name_utf = (*env)->GetStringUTFChars(env, file_name, NULL);
50+
51+
if (source_code_utf != NULL && file_name_utf != NULL) {
52+
JSValue val = JS_Eval(ctx, source_code_utf, (size_t) source_code_length, file_name_utf, flags);
53+
// TODO val to result
54+
JS_FreeValue(ctx, val);
55+
}
56+
57+
if (source_code_utf != NULL) {
58+
(*env)->ReleaseStringUTFChars(env, source_code, source_code_utf);
59+
}
60+
if (file_name_utf != NULL) {
61+
(*env)->ReleaseStringUTFChars(env, file_name, file_name_utf);
62+
}
63+
return result;
64+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright 2019 Hippo Seven
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.hippo.quickjs.android;
18+
19+
import java.io.Closeable;
20+
21+
public class JSContext implements Closeable {
22+
23+
private long context;
24+
25+
JSContext(long context) {
26+
this.context = context;
27+
}
28+
29+
private void checkClosed() {
30+
if (context == 0) {
31+
throw new IllegalStateException("The JSContext is closed");
32+
}
33+
}
34+
35+
public synchronized Object evaluate(String script, String fileName, int flags) {
36+
checkClosed();
37+
return QuickJS.evaluate(context, script, fileName, flags);
38+
}
39+
40+
public synchronized void close() {
41+
if (context != 0) {
42+
long contextToClose = context;
43+
context = 0;
44+
QuickJS.destroyContext(contextToClose);
45+
}
46+
}
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright 2019 Hippo Seven
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.hippo.quickjs.android;
18+
19+
import java.io.Closeable;
20+
21+
public class JSRuntime implements Closeable {
22+
23+
public static JSRuntime create() {
24+
long runtime = QuickJS.createRuntime();
25+
if (runtime == 0) {
26+
throw new QuickJSException("Cannot create JSRuntime instance");
27+
}
28+
return new JSRuntime(runtime);
29+
}
30+
31+
private long runtime;
32+
33+
private JSRuntime(long runtime) {
34+
this.runtime = runtime;
35+
}
36+
37+
private void checkClosed() {
38+
if (runtime == 0) {
39+
throw new IllegalStateException("The JSRuntime is closed");
40+
}
41+
}
42+
43+
public synchronized JSContext createContext() {
44+
checkClosed();
45+
long context = QuickJS.createContext(runtime);
46+
if (context == 0) {
47+
throw new QuickJSException("Cannot create JSContext instance");
48+
}
49+
return new JSContext(context);
50+
}
51+
52+
@Override
53+
public synchronized void close() {
54+
if (runtime != 0) {
55+
long runtimeToClose = runtime;
56+
runtime = 0;
57+
QuickJS.destroyRuntime(runtimeToClose);
58+
}
59+
}
60+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright 2019 Hippo Seven
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.hippo.quickjs.android;
18+
19+
class QuickJS {
20+
21+
static {
22+
System.loadLibrary("quickjs-android");
23+
}
24+
25+
static native long createRuntime();
26+
static native void destroyRuntime(long runtime);
27+
28+
static native long createContext(long runtime);
29+
static native void destroyContext(long context);
30+
31+
static native Object evaluate(long context, String sourceCode, String fileName, int flags);
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright 2019 Hippo Seven
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.hippo.quickjs.android;
18+
19+
public class QuickJSException extends RuntimeException {
20+
21+
public QuickJSException(String detailMessage) {
22+
super(detailMessage);
23+
}
24+
}

quickjs/CMakeLists.txt

+10-2
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,19 @@ set(COMMON_FLAGS -D_GNU_SOURCE -DCONFIG_VERSION=\"${CONFIG_VERSION}\" -DCONFIG_C
1616

1717
set(BN_FLAGS -DCONFIG_BIGNUM)
1818

19-
set(QJS_LIB_SOURCES
19+
set(QUICKJS_LIB_SOURCES
2020
quickjs/quickjs.c
2121
quickjs/libregexp.c
2222
quickjs/libunicode.c
2323
quickjs/cutils.c
24-
quickjs/quickjs-libc.c
2524
patch.c
2625
)
2726

27+
set(QJS_LIB_SOURCES
28+
quickjs/quickjs-libc.c
29+
${QUICKJS_LIB_SOURCES}
30+
)
31+
2832
set(QJSBN_LIB_SOURCES
2933
quickjs/libbf.c
3034
${QJS_LIB_SOURCES}
@@ -60,3 +64,7 @@ set_target_properties(run-test262 PROPERTIES OUTPUT_NAME librun-test262.so)
6064
add_executable(run-test262-bn quickjs/run-test262.c ${QJSBN_LIB_SOURCES})
6165
target_compile_options(run-test262-bn PRIVATE ${COMMON_FLAGS} ${BN_FLAGS})
6266
set_target_properties(run-test262-bn PROPERTIES OUTPUT_NAME librun-test262-bn.so)
67+
68+
add_library(quickjs STATIC ${QUICKJS_LIB_SOURCES})
69+
target_compile_options(quickjs PRIVATE ${COMMON_FLAGS})
70+
target_include_directories(quickjs PUBLIC quickjs)

settings.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@
1515
*/
1616

1717
include ':android-test'
18+
include ':library'

0 commit comments

Comments
 (0)