├── app
├── .gitignore
├── src
│ └── main
│ │ ├── assets
│ │ ├── test.js
│ │ ├── sonic.kbc1
│ │ ├── test.kbc1
│ │ └── JsEngineSonicBridge.js
│ │ ├── res
│ │ ├── values
│ │ │ ├── strings.xml
│ │ │ ├── themes.xml
│ │ │ └── colors.xml
│ │ ├── mipmap-hdpi
│ │ │ ├── ic_launcher.webp
│ │ │ └── ic_launcher_round.webp
│ │ ├── mipmap-mdpi
│ │ │ ├── ic_launcher.webp
│ │ │ └── ic_launcher_round.webp
│ │ ├── mipmap-xhdpi
│ │ │ ├── ic_launcher.webp
│ │ │ └── ic_launcher_round.webp
│ │ ├── mipmap-xxhdpi
│ │ │ ├── ic_launcher.webp
│ │ │ └── ic_launcher_round.webp
│ │ ├── mipmap-xxxhdpi
│ │ │ ├── ic_launcher.webp
│ │ │ └── ic_launcher_round.webp
│ │ ├── mipmap-anydpi-v26
│ │ │ ├── ic_launcher.xml
│ │ │ └── ic_launcher_round.xml
│ │ └── drawable
│ │ │ └── ic_launcher_foreground.xml
│ │ ├── java
│ │ └── com
│ │ │ └── shiqi
│ │ │ └── testquickjs
│ │ │ ├── ui
│ │ │ └── theme
│ │ │ │ ├── Color.kt
│ │ │ │ ├── Type.kt
│ │ │ │ └── Theme.kt
│ │ │ └── MainActivity.kt
│ │ └── AndroidManifest.xml
├── proguard-rules.pro
└── build.gradle
├── quickjs-android
├── .gitignore
├── src
│ └── main
│ │ ├── AndroidManifest.xml
│ │ ├── c
│ │ ├── java-object.h
│ │ ├── java-method.h
│ │ ├── java-helper.c
│ │ ├── java-object.c
│ │ └── java-helper.h
│ │ └── java
│ │ └── com
│ │ └── shiqi
│ │ └── quickjs
│ │ ├── PromiseExecutor.java
│ │ ├── JSFunctionCallback.java
│ │ ├── JSNull.java
│ │ ├── JSInternal.java
│ │ ├── JSSymbol.java
│ │ ├── JSUndefined.java
│ │ ├── JSDataException.java
│ │ ├── JSString.java
│ │ ├── JSArray.java
│ │ ├── JSBoolean.java
│ │ ├── JSEvaluationException.java
│ │ ├── JavaType.java
│ │ ├── JSValueAdapter.java
│ │ ├── JSFunction.java
│ │ ├── JSInt.java
│ │ ├── JSException.java
│ │ ├── JSValue.java
│ │ ├── JSNumber.java
│ │ ├── TypeAdapter.java
│ │ ├── ArrayTypeAdapter.java
│ │ ├── JSFloat64.java
│ │ ├── JSArrayBuffer.java
│ │ ├── NativeCleaner.java
│ │ ├── JSRuntime.java
│ │ ├── JNIHelper.java
│ │ └── JSObject.java
├── proguard-rules.pro
├── CMakeLists.txt
└── build.gradle
├── quickjs
├── VERSION
├── .gitattributes
├── examples
│ ├── hello.js
│ ├── hello_module.js
│ ├── test_fib.js
│ ├── fib_module.js
│ ├── test_point.js
│ ├── pi_bigfloat.js
│ ├── pi_bigdecimal.js
│ ├── fib.c
│ └── pi_bigint.js
├── scripts
│ ├── ci.sh
│ ├── build.sh
│ └── test.sh
├── src
│ ├── core
│ │ ├── misc
│ │ │ ├── debug.h
│ │ │ └── debug.c
│ │ ├── builtins
│ │ │ ├── js-json.h
│ │ │ ├── js-map.h
│ │ │ ├── js-atomics.h
│ │ │ ├── js-regexp.h
│ │ │ ├── js-boolean.h
│ │ │ ├── js-promise.h
│ │ │ ├── js-typed-array.h
│ │ │ ├── js-closures.h
│ │ │ ├── js-symbol.h
│ │ │ ├── js-reflect.h
│ │ │ ├── js-math.h
│ │ │ ├── js-generator.h
│ │ │ ├── js-boolean.c
│ │ │ ├── js-async-function.h
│ │ │ ├── js-number.h
│ │ │ ├── js-operator.h
│ │ │ ├── js-proxy.h
│ │ │ └── js-date.h
│ │ ├── memory.h
│ │ ├── bytecode.h
│ │ ├── malloc.h
│ │ ├── ic.h
│ │ ├── module.h
│ │ ├── base.h
│ │ └── exception.h
│ └── CMakeLists.txt
├── .gitignore
├── tests
│ ├── test_promise_gc_crash.js
│ ├── test_worker_module.js
│ ├── test_worker.js
│ ├── test262.patch
│ └── bjson.c
├── unicode_download.sh
├── CMakeLists.txt
├── README.md
├── include
│ └── quickjs
│ │ ├── libregexp-opcode.h
│ │ ├── libregexp.h
│ │ ├── list.h
│ │ └── libunicode.h
├── quickjs-libc.h
├── TODO
├── release.sh
└── Changelog
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── .gitmodules
├── .vscode
├── settings.json
└── launch.json
├── .gitignore
├── settings.gradle
├── LICENSE
├── gradle.properties
├── README.md
└── gradlew.bat
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
--------------------------------------------------------------------------------
/quickjs-android/.gitignore:
--------------------------------------------------------------------------------
1 | /build
--------------------------------------------------------------------------------
/quickjs/VERSION:
--------------------------------------------------------------------------------
1 | 2021-03-27
2 |
--------------------------------------------------------------------------------
/quickjs/.gitattributes:
--------------------------------------------------------------------------------
1 | * text=auto eol=lf
--------------------------------------------------------------------------------
/app/src/main/assets/test.js:
--------------------------------------------------------------------------------
1 | console.log('Hello, World!')
--------------------------------------------------------------------------------
/quickjs/examples/hello.js:
--------------------------------------------------------------------------------
1 | console.log("Hello World");
2 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | app
3 |
--------------------------------------------------------------------------------
/quickjs/scripts/ci.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | set -x
4 |
5 | bash scripts/build.sh
6 | bash scripts/test.sh
--------------------------------------------------------------------------------
/quickjs-android/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/app/src/main/assets/sonic.kbc1:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/OpenQuickJS/quickjs-android/HEAD/app/src/main/assets/sonic.kbc1
--------------------------------------------------------------------------------
/app/src/main/assets/test.kbc1:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/OpenQuickJS/quickjs-android/HEAD/app/src/main/assets/test.kbc1
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/OpenQuickJS/quickjs-android/HEAD/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/.gitmodules:
--------------------------------------------------------------------------------
1 | [submodule "mimalloc"]
2 | path = quickjs/vendor/mimalloc
3 | url = https://github.com/microsoft/mimalloc.git
4 | branch = v2.1.2
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/OpenQuickJS/quickjs-android/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.webp
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/OpenQuickJS/quickjs-android/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.webp
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/OpenQuickJS/quickjs-android/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.webp
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/OpenQuickJS/quickjs-android/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.webp
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/OpenQuickJS/quickjs-android/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.webp
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher_round.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/OpenQuickJS/quickjs-android/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher_round.webp
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher_round.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/OpenQuickJS/quickjs-android/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher_round.webp
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher_round.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/OpenQuickJS/quickjs-android/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher_round.webp
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/OpenQuickJS/quickjs-android/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.webp
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/OpenQuickJS/quickjs-android/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.webp
--------------------------------------------------------------------------------
/quickjs/examples/hello_module.js:
--------------------------------------------------------------------------------
1 | /* example of JS module */
2 |
3 | import { fib } from "./fib_module.js";
4 |
5 | console.log("Hello World");
6 | console.log("fib(10)=", fib(10));
7 |
--------------------------------------------------------------------------------
/quickjs/src/core/misc/debug.h:
--------------------------------------------------------------------------------
1 | #ifdef CONFIG_DEBUG_ON_RELEASE
2 |
3 | #include
4 | #include
5 |
6 | void __internal_debug_log(const char* fmt, ...);
7 |
8 | #endif
9 |
--------------------------------------------------------------------------------
/quickjs/examples/test_fib.js:
--------------------------------------------------------------------------------
1 | /* example of JS module importing a C module */
2 |
3 | import { fib } from "./fib.so";
4 |
5 | console.log("Hello World");
6 | console.log("fib(10)=", fib(10));
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/themes.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/quickjs/.gitignore:
--------------------------------------------------------------------------------
1 | build
2 | bin
3 | .idea
4 | lib
5 | cmake-build-*
6 | perf.*
7 | build
8 | *.expand
9 | *.svg
10 | test262_errors.txt
11 | test262_report.txt
12 | test262
13 | test262/**
14 | CMakeFiles
--------------------------------------------------------------------------------
/.vscode/settings.json:
--------------------------------------------------------------------------------
1 | {
2 | "cmake.sourceDirectory": "${workspaceFolder}/quickjs",
3 | "cmake.cmakePath": "/usr/local/bin/cmake",
4 | "files.associations": {
5 | "js-array.h": "c"
6 | }
7 | }
--------------------------------------------------------------------------------
/quickjs/scripts/build.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | set -x
4 |
5 | rm -rf build bin lib
6 | mkdir build
7 | cmake -G Ninja -DCMAKE_BUILD_TYPE=Release -B build -S .
8 | cmake --build ./build --target qjs run-test262 -j 8
--------------------------------------------------------------------------------
/quickjs/examples/fib_module.js:
--------------------------------------------------------------------------------
1 | /* fib module */
2 | export function fib(n)
3 | {
4 | if (n <= 0)
5 | return 0;
6 | else if (n == 1)
7 | return 1;
8 | else
9 | return fib(n - 1) + fib(n - 2);
10 | }
11 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Wed Jun 07 16:35:15 CST 2023
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | distributionUrl=https\://services.gradle.org/distributions/gradle-8.0-bin.zip
5 | zipStoreBase=GRADLE_USER_HOME
6 | zipStorePath=wrapper/dists
7 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea/caches
5 | /.idea/libraries
6 | /.idea/modules.xml
7 | /.idea/workspace.xml
8 | /.idea/navEditor.xml
9 | /.idea/assetWizardSettings.xml
10 | .DS_Store
11 | /build
12 | /captures
13 | .externalNativeBuild
14 | .cxx
15 | local.properties
16 | /.idea/
--------------------------------------------------------------------------------
/quickjs/src/core/misc/debug.c:
--------------------------------------------------------------------------------
1 | #ifdef CONFIG_DEBUG_ON_RELEASE
2 | #include "debug.h"
3 |
4 | // log messages to `qjs` repl output
5 |
6 | void __internal_debug_log(const char* fmt, ...) {
7 | va_list argp;
8 | va_start(argp, fmt);
9 |
10 | vprintf(fmt, argp);
11 |
12 | va_end(argp);
13 | printf("\n");
14 | }
15 | #endif
--------------------------------------------------------------------------------
/quickjs/scripts/test.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | set -x
4 |
5 | if [ ! -d test262 ]; then
6 | git clone https://github.com/tc39/test262.git test262
7 | fi
8 |
9 | cd test262
10 | # git checkout ac1c3546c393d89b37483c3a32eddfe7dd1903a7
11 | # patch -p1 < ../tests/test262.patch
12 | cd ..
13 | touch test262_errors.txt
14 | ./bin/run-test262 -m -c test262.conf -a
--------------------------------------------------------------------------------
/quickjs/tests/test_promise_gc_crash.js:
--------------------------------------------------------------------------------
1 | async function createTask() {
2 | return Promise.resolve().then(function () {
3 | new Uint8Array(1000000)
4 | })
5 | }
6 |
7 | run()
8 | async function run() {
9 | let fn = (v) => { console.log(v.length); }
10 | let done = (v) => fn(v)
11 | createTask().then(done)
12 | const p = new Promise(() => { })
13 | await p
14 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/shiqi/testquickjs/ui/theme/Color.kt:
--------------------------------------------------------------------------------
1 | package com.shiqi.testquickjs.ui.theme
2 |
3 | import androidx.compose.ui.graphics.Color
4 |
5 | val Purple80 = Color(0xFFD0BCFF)
6 | val PurpleGrey80 = Color(0xFFCCC2DC)
7 | val Pink80 = Color(0xFFEFB8C8)
8 |
9 | val Purple40 = Color(0xFF6650a4)
10 | val PurpleGrey40 = Color(0xFF625b71)
11 | val Pink40 = Color(0xFF7D5260)
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | pluginManagement {
2 | repositories {
3 | google()
4 | mavenCentral()
5 | gradlePluginPortal()
6 | }
7 | }
8 | dependencyResolutionManagement {
9 | repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
10 | repositories {
11 | google()
12 | mavenCentral()
13 | }
14 | }
15 | rootProject.name = "QuickJS"
16 | include ':app', ':quickjs-android'
17 |
--------------------------------------------------------------------------------
/quickjs-android/src/main/c/java-object.h:
--------------------------------------------------------------------------------
1 | #ifndef QUICKJS_ANDROID_JAVA_OBJECT_H
2 | #define QUICKJS_ANDROID_JAVA_OBJECT_H
3 |
4 | #include
5 | #include
6 |
7 | int java_object_init_context(JSContext *ctx);
8 |
9 | JSValue QJ_NewJavaObject(JSContext *ctx, JNIEnv *env, jobject object);
10 |
11 | jobject QJ_GetJavaObject(JSContext *ctx, JSValueConst val);
12 |
13 | #endif //QUICKJS_ANDROID_JAVA_OBJECT_H
14 |
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #FFBB86FC
4 | #FF6200EE
5 | #FF3700B3
6 | #FF03DAC5
7 | #FF018786
8 | #FF000000
9 | #FFFFFFFF
10 |
--------------------------------------------------------------------------------
/.vscode/launch.json:
--------------------------------------------------------------------------------
1 | {
2 | // Use IntelliSense to learn about possible attributes.
3 | // Hover to view descriptions of existing attributes.
4 | // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5 | "version": "0.2.0",
6 | "configurations": [
7 | {
8 | "type": "lldb",
9 | "request": "launch",
10 | "name": "Debug",
11 | "program": "${workspaceFolder}/quickjs/bin/qjs",
12 | "args": [],
13 | "cwd": "${workspaceFolder}"
14 | }
15 | ]
16 | }
17 |
--------------------------------------------------------------------------------
/quickjs-android/src/main/c/java-method.h:
--------------------------------------------------------------------------------
1 | #ifndef QUICKJS_ANDROID_JAVA_METHOD_H
2 | #define QUICKJS_ANDROID_JAVA_METHOD_H
3 |
4 | #include
5 | #include
6 |
7 | int java_method_init(JNIEnv *env);
8 |
9 | int java_method_init_context(JSContext *ctx);
10 |
11 | JSValue QJ_NewJavaMethod(JSContext *ctx, JNIEnv *env, jobject js_context, jboolean is_static, jobject callee, jmethodID method, jobject return_type, int arg_count, jobject *arg_types, jboolean is_callback_method);
12 |
13 | #endif //QUICKJS_ANDROID_JAVA_METHOD_H
14 |
--------------------------------------------------------------------------------
/quickjs/unicode_download.sh:
--------------------------------------------------------------------------------
1 | #!/bin/sh
2 | set -e
3 |
4 | url="ftp://ftp.unicode.org/Public/14.0.0/ucd"
5 | emoji_url="${url}/emoji/emoji-data.txt"
6 |
7 | files="CaseFolding.txt DerivedNormalizationProps.txt PropList.txt \
8 | SpecialCasing.txt CompositionExclusions.txt ScriptExtensions.txt \
9 | UnicodeData.txt DerivedCoreProperties.txt NormalizationTest.txt Scripts.txt \
10 | PropertyValueAliases.txt"
11 |
12 | mkdir -p unicode
13 |
14 | for f in $files; do
15 | g="${url}/${f}"
16 | wget $g -O unicode/$f
17 | done
18 |
19 | wget $emoji_url -O unicode/emoji-data.txt
20 |
--------------------------------------------------------------------------------
/quickjs-android/src/main/c/java-helper.c:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | #include "java-helper.h"
4 |
5 | #define MAX_MSG_SIZE 1024
6 |
7 | jint throw_exception(JNIEnv *env, const char *exception_name, const char *message, ...) {
8 | char formatted_message[MAX_MSG_SIZE];
9 | va_list va_args;
10 | va_start(va_args, message);
11 | vsnprintf(formatted_message, MAX_MSG_SIZE, message, va_args);
12 | va_end(va_args);
13 |
14 | jclass exception_class = (*env)->FindClass(env, exception_name);
15 | if (exception_class == NULL) {
16 | return -1;
17 | }
18 |
19 | return (*env)->ThrowNew(env, exception_class, formatted_message);
20 | }
21 |
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
--------------------------------------------------------------------------------
/quickjs/tests/test_worker_module.js:
--------------------------------------------------------------------------------
1 | /* Worker code for test_worker.js */
2 | import * as std from "std";
3 | import * as os from "os";
4 |
5 | var parent = os.Worker.parent;
6 |
7 | function handle_msg(e) {
8 | var ev = e.data;
9 | // print("child_recv", JSON.stringify(ev));
10 | switch(ev.type) {
11 | case "abort":
12 | parent.postMessage({ type: "done" });
13 | break;
14 | case "sab":
15 | /* modify the SharedArrayBuffer */
16 | ev.buf[2] = 10;
17 | parent.postMessage({ type: "sab_done", buf: ev.buf });
18 | break;
19 | }
20 | }
21 |
22 | function worker_main() {
23 | var i;
24 |
25 | parent.onmessage = handle_msg;
26 | for(i = 0; i < 10; i++) {
27 | parent.postMessage({ type: "num", num: i });
28 | }
29 | }
30 |
31 | worker_main();
32 |
--------------------------------------------------------------------------------
/quickjs-android/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
--------------------------------------------------------------------------------
/quickjs-android/src/main/java/com/shiqi/quickjs/PromiseExecutor.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020 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.shiqi.quickjs;
18 |
19 | public interface PromiseExecutor {
20 | void execute(JSFunction resolve, JSFunction reject);
21 | }
22 |
--------------------------------------------------------------------------------
/quickjs-android/src/main/java/com/shiqi/quickjs/JSFunctionCallback.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2021 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.shiqi.quickjs;
18 |
19 | public interface JSFunctionCallback {
20 | JSValue invoke(JSContext context, JSValue[] args);
21 | }
22 |
--------------------------------------------------------------------------------
/quickjs/examples/test_point.js:
--------------------------------------------------------------------------------
1 | /* example of JS module importing a C module */
2 | import { Point } from "./point.so";
3 |
4 | function assert(b, str)
5 | {
6 | if (b) {
7 | return;
8 | } else {
9 | throw Error("assertion failed: " + str);
10 | }
11 | }
12 |
13 | class ColorPoint extends Point {
14 | constructor(x, y, color) {
15 | super(x, y);
16 | this.color = color;
17 | }
18 | get_color() {
19 | return this.color;
20 | }
21 | };
22 |
23 | function main()
24 | {
25 | var pt, pt2;
26 |
27 | pt = new Point(2, 3);
28 | assert(pt.x === 2);
29 | assert(pt.y === 3);
30 | pt.x = 4;
31 | assert(pt.x === 4);
32 | assert(pt.norm() == 5);
33 |
34 | pt2 = new ColorPoint(2, 3, 0xffffff);
35 | assert(pt2.x === 2);
36 | assert(pt2.color === 0xffffff);
37 | assert(pt2.get_color() === 0xffffff);
38 | }
39 |
40 | main();
41 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
11 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
--------------------------------------------------------------------------------
/quickjs-android/src/main/java/com/shiqi/quickjs/JSNull.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2019 Hippo Seven
3 | * Copyright 2023-Present Shiqi Mei
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package com.shiqi.quickjs;
19 |
20 | /**
21 | * JavaScript null.
22 | */
23 | public final class JSNull extends JSValue {
24 |
25 | JSNull(long pointer, JSContext jsContext) {
26 | super(pointer, jsContext);
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/quickjs-android/src/main/java/com/shiqi/quickjs/JSInternal.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2019 Hippo Seven
3 | * Copyright 2023-Present Shiqi Mei
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package com.shiqi.quickjs;
19 |
20 | /**
21 | * Used internally in QuickJS.
22 | */
23 | class JSInternal extends JSValue {
24 |
25 | JSInternal(long pointer, JSContext jsContext) {
26 | super(pointer, jsContext);
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/quickjs-android/src/main/java/com/shiqi/quickjs/JSSymbol.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2019 Hippo Seven
3 | * Copyright 2023-Present Shiqi Mei
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package com.shiqi.quickjs;
19 |
20 | /**
21 | * JavaScript symbol.
22 | */
23 | public final class JSSymbol extends JSValue {
24 |
25 | JSSymbol(long pointer, JSContext jsContext) {
26 | super(pointer, jsContext);
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/quickjs-android/src/main/java/com/shiqi/quickjs/JSUndefined.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2019 Hippo Seven
3 | * Copyright 2023-Present Shiqi Mei
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package com.shiqi.quickjs;
19 |
20 | /**
21 | * JavaScript undefined.
22 | */
23 | public final class JSUndefined extends JSValue {
24 |
25 | JSUndefined(long pointer, JSContext jsContext) {
26 | super(pointer, jsContext);
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/quickjs-android/CMakeLists.txt:
--------------------------------------------------------------------------------
1 | cmake_minimum_required(VERSION 3.4.1)
2 |
3 | project(quickjs-android)
4 |
5 | if (CMAKE_BUILD_TYPE STREQUAL "Debug")
6 | option(LEAK_TRIGGER "Add a leak trigger" ON)
7 | else ()
8 | option(LEAK_TRIGGER "Add a leak trigger" OFF)
9 | endif (CMAKE_BUILD_TYPE STREQUAL "Debug")
10 |
11 | include_directories(../quickjs/include)
12 | add_subdirectory(../quickjs ${CMAKE_CURRENT_BINARY_DIR}/quickjs)
13 |
14 | set(QUICKJS_ANDROID_SOURCES
15 | src/main/c/quickjs-jni.c
16 | src/main/c/java-method.c
17 | src/main/c/java-object.c
18 | src/main/c/java-helper.c
19 | )
20 |
21 | if (LEAK_TRIGGER)
22 | set(COMMON_FLAGS -DLEAK_TRIGGER)
23 | else ()
24 | set(COMMON_FLAGS)
25 | endif (LEAK_TRIGGER)
26 |
27 | add_library(quickjs-android SHARED ${QUICKJS_ANDROID_SOURCES})
28 | find_library(log-lib log)
29 | target_compile_options(quickjs-android PRIVATE ${COMMON_FLAGS})
30 | target_link_libraries(quickjs-android PRIVATE quickjs mimalloc ${log-lib})
31 |
--------------------------------------------------------------------------------
/quickjs-android/src/main/java/com/shiqi/quickjs/JSDataException.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2019 Hippo Seven
3 | * Copyright 2023-Present Shiqi Mei
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package com.shiqi.quickjs;
19 |
20 | /**
21 | * This exception is raised if JSValue can't be convert to Java type.
22 | */
23 | public class JSDataException extends RuntimeException {
24 | public JSDataException(String message) {
25 | super(message);
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/quickjs-android/src/main/java/com/shiqi/quickjs/JSString.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2019 Hippo Seven
3 | * Copyright 2023-Present Shiqi Mei
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package com.shiqi.quickjs;
19 |
20 | /**
21 | * JavaScript string.
22 | */
23 | public final class JSString extends JSValue {
24 |
25 | private final String value;
26 |
27 | JSString(long pointer, JSContext jsContext, String value) {
28 | super(pointer, jsContext);
29 | this.value = value;
30 | }
31 |
32 | public String getString() {
33 | return value;
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/app/src/main/java/com/shiqi/testquickjs/ui/theme/Type.kt:
--------------------------------------------------------------------------------
1 | package com.shiqi.testquickjs.ui.theme
2 |
3 | import androidx.compose.material3.Typography
4 | import androidx.compose.ui.text.TextStyle
5 | import androidx.compose.ui.text.font.FontFamily
6 | import androidx.compose.ui.text.font.FontWeight
7 | import androidx.compose.ui.unit.sp
8 |
9 | // Set of Material typography styles to start with
10 | val Typography = Typography(
11 | bodyLarge = TextStyle(
12 | fontFamily = FontFamily.Default,
13 | fontWeight = FontWeight.Normal,
14 | fontSize = 16.sp,
15 | lineHeight = 24.sp,
16 | letterSpacing = 0.5.sp
17 | )
18 | /* Other default text styles to override
19 | titleLarge = TextStyle(
20 | fontFamily = FontFamily.Default,
21 | fontWeight = FontWeight.Normal,
22 | fontSize = 22.sp,
23 | lineHeight = 28.sp,
24 | letterSpacing = 0.sp
25 | ),
26 | labelSmall = TextStyle(
27 | fontFamily = FontFamily.Default,
28 | fontWeight = FontWeight.Medium,
29 | fontSize = 11.sp,
30 | lineHeight = 16.sp,
31 | letterSpacing = 0.5.sp
32 | )
33 | */
34 | )
--------------------------------------------------------------------------------
/quickjs-android/src/main/java/com/shiqi/quickjs/JSArray.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2019 Hippo Seven
3 | * Copyright 2023-Present Shiqi Mei
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package com.shiqi.quickjs;
19 |
20 | /**
21 | * JavaScript array.
22 | */
23 | public final class JSArray extends JSObject {
24 |
25 | JSArray(long pointer, JSContext jsContext) {
26 | super(pointer, jsContext, null);
27 | }
28 |
29 | /**
30 | * Returns the number of elements in an array.
31 | */
32 | public int getLength() {
33 | return getProperty("length").cast(JSNumber.class).getInt();
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/quickjs-android/src/main/java/com/shiqi/quickjs/JSBoolean.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2019 Hippo Seven
3 | * Copyright 2023-Present Shiqi Mei
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package com.shiqi.quickjs;
19 |
20 | /**
21 | * JavaScript boolean.
22 | */
23 | public class JSBoolean extends JSValue {
24 |
25 | private final boolean value;
26 |
27 | JSBoolean(long pointer, JSContext jsContext, boolean value) {
28 | super(pointer, jsContext);
29 | this.value = value;
30 | }
31 |
32 | /**
33 | * Returns boolean value.
34 | */
35 | public boolean getBoolean() {
36 | return value;
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/quickjs-android/src/main/java/com/shiqi/quickjs/JSEvaluationException.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2019 Hippo Seven
3 | * Copyright 2023-Present Shiqi Mei
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package com.shiqi.quickjs;
19 |
20 | /**
21 | * This exception is raised if QuickJS raises a JavaScript exception.
22 | */
23 | public class JSEvaluationException extends RuntimeException {
24 |
25 | private JSException jsException;
26 |
27 | JSEvaluationException(JSException jsException) {
28 | super(jsException.toString());
29 | }
30 |
31 | public JSException getJSException() {
32 | return jsException;
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | QuickJS Javascript Engine
2 |
3 | Copyright (c) 2017-2021 Fabrice Bellard
4 | Copyright (c) 2017-2021 Charlie Gordon
5 |
6 | Permission is hereby granted, free of charge, to any person obtaining a copy
7 | of this software and associated documentation files (the "Software"), to deal
8 | in the Software without restriction, including without limitation the rights
9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | copies of the Software, and to permit persons to whom the Software is
11 | furnished to do so, subject to the following conditions:
12 |
13 | The above copyright notice and this permission notice shall be included in
14 | all copies or substantial portions of the Software.
15 |
16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 | THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | THE SOFTWARE.
23 |
--------------------------------------------------------------------------------
/quickjs/src/core/builtins/js-json.h:
--------------------------------------------------------------------------------
1 | /*
2 | * QuickJS Javascript Engine
3 | *
4 | * Copyright (c) 2017-2021 Fabrice Bellard
5 | * Copyright (c) 2017-2021 Charlie Gordon
6 | *
7 | * Permission is hereby granted, free of charge, to any person obtaining a copy
8 | * of this software and associated documentation files (the "Software"), to deal
9 | * in the Software without restriction, including without limitation the rights
10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 | * copies of the Software, and to permit persons to whom the Software is
12 | * furnished to do so, subject to the following conditions:
13 | *
14 | * The above copyright notice and this permission notice shall be included in
15 | * all copies or substantial portions of the Software.
16 | *
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 | * THE SOFTWARE.
24 | */
25 |
26 | #ifndef QUICKJS_JS_JSON_H
27 | #define QUICKJS_JS_JSON_H
28 |
29 | #include "quickjs/quickjs.h"
30 |
31 | #endif
--------------------------------------------------------------------------------
/quickjs/src/core/memory.h:
--------------------------------------------------------------------------------
1 | /*
2 | * QuickJS Javascript Engine
3 | *
4 | * Copyright (c) 2017-2021 Fabrice Bellard
5 | * Copyright (c) 2017-2021 Charlie Gordon
6 | *
7 | * Permission is hereby granted, free of charge, to any person obtaining a copy
8 | * of this software and associated documentation files (the "Software"), to deal
9 | * in the Software without restriction, including without limitation the rights
10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 | * copies of the Software, and to permit persons to whom the Software is
12 | * furnished to do so, subject to the following conditions:
13 | *
14 | * The above copyright notice and this permission notice shall be included in
15 | * all copies or substantial portions of the Software.
16 | *
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 | * THE SOFTWARE.
24 | */
25 |
26 | #ifndef QUICKJS_MEMORY_H
27 | #define QUICKJS_MEMORY_H
28 |
29 | #include "quickjs/quickjs.h"
30 | #include "quickjs/cutils.h"
31 |
32 | #endif
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 | # IDE (e.g. Android Studio) users:
3 | # Gradle settings configured through the IDE *will override*
4 | # any settings specified in this file.
5 | # For more details on how to configure your build environment visit
6 | # http://www.gradle.org/docs/current/userguide/build_environment.html
7 | # Specifies the JVM arguments used for the daemon process.
8 | # The setting is particularly useful for tweaking memory settings.
9 | org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8
10 | # When configured, Gradle will run in incubating parallel mode.
11 | # This option should only be used with decoupled projects. More details, visit
12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
13 | # org.gradle.parallel=true
14 | # AndroidX package structure to make it clearer which packages are bundled with the
15 | # Android operating system, and which are packaged with your app's APK
16 | # https://developer.android.com/topic/libraries/support-library/androidx-rn
17 | android.useAndroidX=true
18 | # Kotlin code style for this project: "official" or "obsolete":
19 | kotlin.code.style=official
20 | # Enables namespacing of each library's R class so that its R class includes only the
21 | # resources declared in the library itself and none from the library's dependencies,
22 | # thereby reducing the size of the R class for that library
23 | android.nonTransitiveRClass=true
--------------------------------------------------------------------------------
/quickjs/src/core/builtins/js-map.h:
--------------------------------------------------------------------------------
1 | /*
2 | * QuickJS Javascript Engine
3 | *
4 | * Copyright (c) 2017-2021 Fabrice Bellard
5 | * Copyright (c) 2017-2021 Charlie Gordon
6 | *
7 | * Permission is hereby granted, free of charge, to any person obtaining a copy
8 | * of this software and associated documentation files (the "Software"), to deal
9 | * in the Software without restriction, including without limitation the rights
10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 | * copies of the Software, and to permit persons to whom the Software is
12 | * furnished to do so, subject to the following conditions:
13 | *
14 | * The above copyright notice and this permission notice shall be included in
15 | * all copies or substantial portions of the Software.
16 | *
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 | * THE SOFTWARE.
24 | */
25 |
26 | #ifndef QUICKJS_JS_MAP_H
27 | #define QUICKJS_JS_MAP_H
28 |
29 | #include "quickjs/quickjs.h"
30 |
31 | void reset_weak_ref(JSRuntime *rt, JSObject *p);
32 |
33 | #endif
--------------------------------------------------------------------------------
/quickjs-android/src/main/java/com/shiqi/quickjs/JavaType.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2021 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.shiqi.quickjs;
18 |
19 | import androidx.annotation.NonNull;
20 |
21 | import java.lang.reflect.ParameterizedType;
22 | import java.lang.reflect.Type;
23 |
24 | public abstract class JavaType {
25 | @NonNull
26 | public Type type;
27 |
28 | public JavaType() {
29 | Type supertype = JavaTypes.canonicalize(getClass().getGenericSuperclass());
30 | if (!(supertype instanceof ParameterizedType)) invalidJavaType();
31 |
32 | Type[] types = ((ParameterizedType) supertype).getActualTypeArguments();
33 | if (types.length != 1) invalidJavaType();
34 |
35 | type = types[0];
36 | }
37 |
38 | private void invalidJavaType() {
39 | throw new IllegalStateException(
40 | "Invalid JavaType. JavaType must be inherited by a anonymous class");
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/quickjs/src/core/builtins/js-atomics.h:
--------------------------------------------------------------------------------
1 | /*
2 | * QuickJS Javascript Engine
3 | *
4 | * Copyright (c) 2017-2021 Fabrice Bellard
5 | * Copyright (c) 2017-2021 Charlie Gordon
6 | *
7 | * Permission is hereby granted, free of charge, to any person obtaining a copy
8 | * of this software and associated documentation files (the "Software"), to deal
9 | * in the Software without restriction, including without limitation the rights
10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 | * copies of the Software, and to permit persons to whom the Software is
12 | * furnished to do so, subject to the following conditions:
13 | *
14 | * The above copyright notice and this permission notice shall be included in
15 | * all copies or substantial portions of the Software.
16 | *
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 | * THE SOFTWARE.
24 | */
25 |
26 | #ifndef QUICKJS_JS_ATOMICS_H
27 | #define QUICKJS_JS_ATOMICS_H
28 |
29 | #include "quickjs/quickjs.h"
30 |
31 | #ifdef CONFIG_ATOMICS
32 | void JS_AddIntrinsicAtomics(JSContext *ctx);
33 | #endif
34 |
35 |
36 | #endif
--------------------------------------------------------------------------------
/quickjs-android/src/main/java/com/shiqi/quickjs/JSValueAdapter.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2019 Hippo Seven
3 | * Copyright 2023-Present Shiqi Mei
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package com.shiqi.quickjs;
19 |
20 | import androidx.annotation.Nullable;
21 |
22 | import java.lang.reflect.Type;
23 |
24 | class JSValueAdapter {
25 |
26 | static final TypeAdapter.Factory FACTORY = new TypeAdapter.Factory() {
27 |
28 | @Nullable
29 | @Override
30 | public TypeAdapter> create(QuickJS quickJS, Type type) {
31 | if (type == JSValue.class) return JS_VALUE_TYPE_ADAPTER;
32 | return null;
33 | }
34 | };
35 |
36 | private static final TypeAdapter JS_VALUE_TYPE_ADAPTER = new TypeAdapter() {
37 | @Override
38 | public JSValue toJSValue(JSContext context, JSValue value) {
39 | if (value == null) throw new NullPointerException("value == null");
40 | return value;
41 | }
42 |
43 | @Override
44 | public JSValue fromJSValue(JSContext context, JSValue value) {
45 | return value;
46 | }
47 | };
48 | }
49 |
--------------------------------------------------------------------------------
/quickjs/src/core/builtins/js-regexp.h:
--------------------------------------------------------------------------------
1 | /*
2 | * QuickJS Javascript Engine
3 | *
4 | * Copyright (c) 2017-2021 Fabrice Bellard
5 | * Copyright (c) 2017-2021 Charlie Gordon
6 | *
7 | * Permission is hereby granted, free of charge, to any person obtaining a copy
8 | * of this software and associated documentation files (the "Software"), to deal
9 | * in the Software without restriction, including without limitation the rights
10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 | * copies of the Software, and to permit persons to whom the Software is
12 | * furnished to do so, subject to the following conditions:
13 | *
14 | * The above copyright notice and this permission notice shall be included in
15 | * all copies or substantial portions of the Software.
16 | *
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 | * THE SOFTWARE.
24 | */
25 |
26 | #ifndef QUICKJS_JS_REGEXP_H
27 | #define QUICKJS_JS_REGEXP_H
28 |
29 | #include "quickjs/quickjs.h"
30 |
31 | JSValue js_compile_regexp(JSContext *ctx, JSValueConst pattern,
32 | JSValueConst flags);
33 | JSValue js_regexp_constructor_internal(JSContext *ctx, JSValueConst ctor,
34 | JSValue pattern, JSValue bc);
35 |
36 | #endif
--------------------------------------------------------------------------------
/quickjs/src/core/bytecode.h:
--------------------------------------------------------------------------------
1 | /*
2 | * QuickJS Javascript Engine
3 | *
4 | * Copyright (c) 2017-2021 Fabrice Bellard
5 | * Copyright (c) 2017-2021 Charlie Gordon
6 | *
7 | * Permission is hereby granted, free of charge, to any person obtaining a copy
8 | * of this software and associated documentation files (the "Software"), to deal
9 | * in the Software without restriction, including without limitation the rights
10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 | * copies of the Software, and to permit persons to whom the Software is
12 | * furnished to do so, subject to the following conditions:
13 | *
14 | * The above copyright notice and this permission notice shall be included in
15 | * all copies or substantial portions of the Software.
16 | *
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 | * THE SOFTWARE.
24 | */
25 |
26 | #ifndef QUICKJS_BYTECODE_H
27 | #define QUICKJS_BYTECODE_H
28 |
29 | #include "quickjs/quickjs.h"
30 | #include "quickjs/cutils.h"
31 | #include "types.h"
32 |
33 | void free_function_bytecode(JSRuntime *rt, JSFunctionBytecode *b);
34 | void free_bytecode_atoms(JSRuntime *rt,
35 | const uint8_t *bc_buf, int bc_len,
36 | BOOL use_short_opcodes);;
37 |
38 | #endif
--------------------------------------------------------------------------------
/quickjs-android/src/main/c/java-object.c:
--------------------------------------------------------------------------------
1 | #include "java-object.h"
2 | #include "java-helper.h"
3 |
4 | static JSClassID java_object_class_id;
5 |
6 | typedef struct {
7 | JavaVM *vm;
8 | jobject object;
9 | } JavaObjectData;
10 |
11 | static void java_object_finalizer(JSRuntime *rt, JSValue val) {
12 | JavaObjectData *data = JS_GetOpaque(val, java_object_class_id);
13 |
14 | OBTAIN_ENV(data->vm);
15 |
16 | if (env != NULL) {
17 | (*env)->DeleteGlobalRef(env, data->object);
18 | }
19 |
20 | RELEASE_ENV(data->vm);
21 |
22 | js_free_rt(rt, data);
23 | }
24 |
25 | static JSClassDef java_object_class = {
26 | "JavaObject",
27 | .finalizer = java_object_finalizer
28 | };
29 |
30 | int java_object_init_context(JSContext *ctx) {
31 | JS_NewClassID(&java_object_class_id);
32 | if (JS_NewClass(JS_GetRuntime(ctx), java_object_class_id, &java_object_class)) return -1;
33 | return 0;
34 | }
35 |
36 | JSValue QJ_NewJavaObject(JSContext *ctx, JNIEnv *env, jobject object) {
37 | JSRuntime *rt = JS_GetRuntime(ctx);
38 |
39 | JavaObjectData *data = js_malloc_rt(rt, sizeof(JavaObjectData));
40 | if (data == NULL) return JS_ThrowOutOfMemory(ctx);
41 |
42 | JSValue value = JS_NewObjectClass(ctx, java_object_class_id);
43 | if (JS_IsException(value)) {
44 | js_free_rt(rt, data);
45 | return value;
46 | }
47 |
48 | (*env)->GetJavaVM(env, &data->vm);
49 | data->object = (*env)->NewGlobalRef(env, object);
50 |
51 | JS_SetOpaque(value, data);
52 |
53 | return value;
54 | }
55 |
56 | jobject QJ_GetJavaObject(JSContext __unused *ctx, JSValueConst val) {
57 | JavaObjectData *data = JS_GetOpaque(val, java_object_class_id);
58 | return data != NULL ? data->object : NULL;
59 | }
60 |
--------------------------------------------------------------------------------
/quickjs-android/src/main/java/com/shiqi/quickjs/JSFunction.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2019 Hippo Seven
3 | * Copyright 2023-Present Shiqi Mei
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package com.shiqi.quickjs;
19 |
20 | import androidx.annotation.Nullable;
21 |
22 | /**
23 | * JavaScript function.
24 | */
25 | public final class JSFunction extends JSObject {
26 |
27 | JSFunction(long pointer, JSContext jsContext) {
28 | super(pointer, jsContext, null);
29 | }
30 |
31 | /**
32 | * Calls the JavaScript function.
33 | */
34 | public JSValue invoke(@Nullable JSValue thisObj, JSValue[] args) {
35 | // Check whether JSValues are from the same JSRuntime
36 | if (thisObj != null) checkSameJSContext(thisObj);
37 | for (JSValue arg : args) checkSameJSContext(arg);
38 |
39 | long[] valueArgs = new long[args.length];
40 | for (int i = 0; i < args.length; i++) {
41 | valueArgs[i] = args[i].pointer;
42 | }
43 |
44 | synchronized (jsContext.jsRuntime) {
45 | long context = jsContext.checkClosed();
46 | long ret = QuickJS.invokeValueFunction(context, pointer, thisObj != null ? thisObj.pointer : 0, valueArgs);
47 | return jsContext.wrapAsJSValue(ret);
48 | }
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/quickjs-android/build.gradle:
--------------------------------------------------------------------------------
1 | plugins {
2 | id 'com.android.library'
3 | id 'org.jetbrains.kotlin.android'
4 | }
5 |
6 | android {
7 | namespace 'com.shiqi.quickjs'
8 | compileSdk 33
9 |
10 | defaultConfig {
11 | minSdk 22
12 | targetSdk 33
13 | versionCode 1
14 | versionName "1.0"
15 |
16 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
17 | vectorDrawables {
18 | useSupportLibrary true
19 | }
20 |
21 | // build quickjs
22 | externalNativeBuild {
23 | cmake {
24 | targets 'quickjs-android'
25 | arguments '-DCMAKE_VERBOSE_MAKEFILE:BOOL=ON'
26 | }
27 | }
28 | ndk {
29 | //noinspection ChromeOsAbiSupport
30 | abiFilters 'armeabi-v7a', 'arm64-v8a'
31 | }
32 | }
33 |
34 | externalNativeBuild {
35 | cmake {
36 | path 'CMakeLists.txt'
37 | }
38 | }
39 |
40 | buildTypes {
41 | release {
42 | minifyEnabled false
43 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
44 | }
45 | }
46 | compileOptions {
47 | sourceCompatibility JavaVersion.VERSION_1_8
48 | targetCompatibility JavaVersion.VERSION_1_8
49 | }
50 | kotlinOptions {
51 | jvmTarget = '1.8'
52 | }
53 | buildFeatures {
54 | compose true
55 | }
56 | composeOptions {
57 | kotlinCompilerExtensionVersion '1.4.5'
58 | }
59 | packagingOptions {
60 | resources {
61 | excludes += '/META-INF/{AL2.0,LGPL2.1}'
62 | }
63 | }
64 | }
65 |
66 | dependencies {
67 | implementation 'androidx.annotation:annotation:1.6.0'
68 | }
--------------------------------------------------------------------------------
/quickjs/src/core/builtins/js-boolean.h:
--------------------------------------------------------------------------------
1 | /*
2 | * QuickJS Javascript Engine
3 | *
4 | * Copyright (c) 2017-2021 Fabrice Bellard
5 | * Copyright (c) 2017-2021 Charlie Gordon
6 | *
7 | * Permission is hereby granted, free of charge, to any person obtaining a copy
8 | * of this software and associated documentation files (the "Software"), to deal
9 | * in the Software without restriction, including without limitation the rights
10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 | * copies of the Software, and to permit persons to whom the Software is
12 | * furnished to do so, subject to the following conditions:
13 | *
14 | * The above copyright notice and this permission notice shall be included in
15 | * all copies or substantial portions of the Software.
16 | *
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 | * THE SOFTWARE.
24 | */
25 |
26 | #ifndef QUICKJS_JS_BOOLEAN_H
27 | #define QUICKJS_JS_BOOLEAN_H
28 |
29 | #include "quickjs/quickjs.h"
30 |
31 | JSValue js_boolean_constructor(JSContext* ctx, JSValueConst new_target, int argc, JSValueConst* argv);
32 | JSValue js_thisBooleanValue(JSContext* ctx, JSValueConst this_val);
33 | JSValue js_boolean_toString(JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv);
34 | JSValue js_boolean_valueOf(JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv);
35 |
36 | #endif
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_launcher_foreground.xml:
--------------------------------------------------------------------------------
1 |
7 |
8 |
9 |
15 |
18 |
21 |
22 |
23 |
24 |
30 |
--------------------------------------------------------------------------------
/quickjs/src/core/builtins/js-promise.h:
--------------------------------------------------------------------------------
1 | /*
2 | * QuickJS Javascript Engine
3 | *
4 | * Copyright (c) 2017-2021 Fabrice Bellard
5 | * Copyright (c) 2017-2021 Charlie Gordon
6 | *
7 | * Permission is hereby granted, free of charge, to any person obtaining a copy
8 | * of this software and associated documentation files (the "Software"), to deal
9 | * in the Software without restriction, including without limitation the rights
10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 | * copies of the Software, and to permit persons to whom the Software is
12 | * furnished to do so, subject to the following conditions:
13 | *
14 | * The above copyright notice and this permission notice shall be included in
15 | * all copies or substantial portions of the Software.
16 | *
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 | * THE SOFTWARE.
24 | */
25 |
26 | #ifndef QUICKJS_JS_PROMISE_H
27 | #define QUICKJS_JS_PROMISE_H
28 |
29 | #include "quickjs/quickjs.h"
30 | #include "../types.h"
31 |
32 | JSValue js_promise_resolve(JSContext *ctx, JSValueConst this_val,
33 | int argc, JSValueConst *argv, int magic);
34 |
35 | __exception int perform_promise_then(JSContext *ctx,
36 | JSValueConst promise,
37 | JSValueConst *resolve_reject,
38 | JSValueConst *cap_resolving_funcs);
39 |
40 | #endif
--------------------------------------------------------------------------------
/quickjs/tests/test_worker.js:
--------------------------------------------------------------------------------
1 | /* os.Worker API test */
2 | import * as std from "std";
3 | import * as os from "os";
4 |
5 | function assert(actual, expected, message) {
6 | if (arguments.length == 1)
7 | expected = true;
8 |
9 | if (actual === expected)
10 | return;
11 |
12 | if (actual !== null && expected !== null
13 | && typeof actual == 'object' && typeof expected == 'object'
14 | && actual.toString() === expected.toString())
15 | return;
16 |
17 | throw Error("assertion failed: got |" + actual + "|" +
18 | ", expected |" + expected + "|" +
19 | (message ? " (" + message + ")" : ""));
20 | }
21 |
22 | var worker;
23 |
24 | function test_worker()
25 | {
26 | var counter;
27 |
28 | worker = new os.Worker("./test_worker_module.js");
29 |
30 | counter = 0;
31 | worker.onmessage = function (e) {
32 | var ev = e.data;
33 | // print("recv", JSON.stringify(ev));
34 | switch(ev.type) {
35 | case "num":
36 | assert(ev.num, counter);
37 | counter++;
38 | if (counter == 10) {
39 | /* test SharedArrayBuffer modification */
40 | let sab = new SharedArrayBuffer(10);
41 | let buf = new Uint8Array(sab);
42 | worker.postMessage({ type: "sab", buf: buf });
43 | }
44 | break;
45 | case "sab_done":
46 | {
47 | let buf = ev.buf;
48 | /* check that the SharedArrayBuffer was modified */
49 | assert(buf[2], 10);
50 | worker.postMessage({ type: "abort" });
51 | }
52 | break;
53 | case "done":
54 | /* terminate */
55 | worker.onmessage = null;
56 | break;
57 | }
58 | };
59 | }
60 |
61 |
62 | test_worker();
63 |
--------------------------------------------------------------------------------
/quickjs-android/src/main/java/com/shiqi/quickjs/JSInt.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2019 Hippo Seven
3 | * Copyright 2023-Present Shiqi Mei
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package com.shiqi.quickjs;
19 |
20 | final class JSInt extends JSNumber {
21 |
22 | private final int value;
23 |
24 | JSInt(long pointer, JSContext jsContext, int value) {
25 | super(pointer, jsContext);
26 | this.value = value;
27 | }
28 |
29 | private int getIntInRange(String javaType, int min, int max) {
30 | int value = this.value;
31 | if (min <= value && value <= max) {
32 | return value;
33 | } else {
34 | throw new JSDataException("Can't treat " + value + " as " + javaType);
35 | }
36 | }
37 |
38 | @Override
39 | public byte getByte() {
40 | return (byte) getIntInRange("byte", Byte.MIN_VALUE, Byte.MAX_VALUE);
41 | }
42 |
43 | @Override
44 | public short getShort() {
45 | return (short) getIntInRange("short", Short.MIN_VALUE, Short.MAX_VALUE);
46 | }
47 |
48 | @Override
49 | public int getInt() {
50 | return value;
51 | }
52 |
53 | @Override
54 | public long getLong() {
55 | return value;
56 | }
57 |
58 | @Override
59 | public float getFloat() {
60 | return value;
61 | }
62 |
63 | @Override
64 | public double getDouble() {
65 | return value;
66 | }
67 | }
68 |
--------------------------------------------------------------------------------
/quickjs-android/src/main/java/com/shiqi/quickjs/JSException.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2019 Hippo Seven
3 | * Copyright 2023-Present Shiqi Mei
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package com.shiqi.quickjs;
19 |
20 | import androidx.annotation.NonNull;
21 | import androidx.annotation.Nullable;
22 |
23 | /**
24 | * The information of JavaScript exception.
25 | */
26 | public class JSException {
27 |
28 | private final boolean isError;
29 | private final String exception;
30 | private final String stack;
31 |
32 | private JSException(boolean isError, String exception, String stack) {
33 | this.isError = isError;
34 | this.exception = exception;
35 | this.stack = stack;
36 | }
37 |
38 | public boolean isError() {
39 | return isError;
40 | }
41 |
42 | /**
43 | * The exception message.
44 | */
45 | @Nullable
46 | public String getException() {
47 | return exception;
48 | }
49 |
50 | /**
51 | * The stack trace.
52 | */
53 | @Nullable
54 | public String getStack() {
55 | return stack;
56 | }
57 |
58 | @NonNull
59 | @Override
60 | public String toString() {
61 | StringBuilder sb = new StringBuilder();
62 | if (!isError) {
63 | sb.append("Throw: ");
64 | }
65 | sb.append(exception).append("\n");
66 | if (stack != null) {
67 | sb.append(stack);
68 | }
69 | return sb.toString();
70 | }
71 | }
72 |
--------------------------------------------------------------------------------
/quickjs-android/src/main/java/com/shiqi/quickjs/JSValue.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2019 Hippo Seven
3 | * Copyright 2023-Present Shiqi Mei
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package com.shiqi.quickjs;
19 |
20 | // TODO Make JSValue closeable?
21 |
22 | /**
23 | * JSValue is a Javascript value.
24 | * It could be a number, a object, null, undefined or something else.
25 | */
26 | public abstract class JSValue {
27 |
28 | final long pointer;
29 | final JSContext jsContext;
30 |
31 | JSValue(long pointer, JSContext jsContext) {
32 | this.pointer = pointer;
33 | this.jsContext = jsContext;
34 | }
35 |
36 | /**
37 | * Cast this JSValue to a special type.
38 | *
39 | * @throws JSDataException if it's not the type
40 | */
41 | @SuppressWarnings("unchecked")
42 | public final T cast(Class clazz) {
43 | if (clazz.isInstance(this)) {
44 | return (T) this;
45 | } else {
46 | throw new JSDataException("expected: " + clazz.getSimpleName() + ", actual: " + getClass().getSimpleName());
47 | }
48 | }
49 |
50 | /**
51 | * @throws IllegalStateException if two JSValues are not from the same JSContext
52 | */
53 | final void checkSameJSContext(JSValue jsValue) {
54 | if (jsValue.jsContext != jsContext) {
55 | throw new IllegalStateException("Two JSValues are not from the same JSContext");
56 | }
57 | }
58 | }
59 |
--------------------------------------------------------------------------------
/app/src/main/assets/JsEngineSonicBridge.js:
--------------------------------------------------------------------------------
1 | var sonicNativeBridgeCore=function(){"use strict";function c(e,r){for(var t=0;t> 1n;
27 | [P1, Q1, G1] = chud_bs(a, c, true);
28 | [P2, Q2, G2] = chud_bs(c, b, need_G);
29 | P = P1 * Q2 + P2 * G1;
30 | Q = Q1 * Q2;
31 | if (need_G)
32 | G = G1 * G2;
33 | else
34 | G = 0l;
35 | }
36 | return [P, Q, G];
37 | }
38 |
39 | var n, P, Q, G;
40 | /* number of serie terms */
41 | n = BigInt(Math.ceil(BigFloatEnv.prec / CHUD_BITS_PER_TERM)) + 10n;
42 | [P, Q, G] = chud_bs(0n, n, false);
43 | Q = Q / (P + Q * BigFloat(CHUD_A));
44 | G = BigFloat((CHUD_C / 12n)) * BigFloat.sqrt(BigFloat(CHUD_C));
45 | return Q * G;
46 | }
47 |
48 | (function() {
49 | var r, n_digits, n_bits;
50 | if (typeof scriptArgs != "undefined") {
51 | if (scriptArgs.length < 2) {
52 | print("usage: pi n_digits");
53 | return;
54 | }
55 | n_digits = scriptArgs[1];
56 | } else {
57 | n_digits = 1000;
58 | }
59 | n_bits = Math.ceil(n_digits * Math.log2(10));
60 | /* we add more bits to reduce the probability of bad rounding for
61 | the last digits */
62 | BigFloatEnv.setPrec( () => {
63 | r = calc_pi();
64 | print(r.toFixed(n_digits, BigFloatEnv.RNDZ));
65 | }, n_bits + 32);
66 | })();
67 |
--------------------------------------------------------------------------------
/quickjs-android/src/main/java/com/shiqi/quickjs/TypeAdapter.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2019 Hippo Seven
3 | * Copyright 2023-Present Shiqi Mei
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package com.shiqi.quickjs;
19 |
20 | import androidx.annotation.Nullable;
21 |
22 | import java.lang.reflect.Type;
23 |
24 | public abstract class TypeAdapter {
25 | /**
26 | * Converts the java value to {@code JSValue}.
27 | * Throws {@link JSDataException} if the value can't be handled.
28 | */
29 | public abstract JSValue toJSValue(JSContext context, T value);
30 |
31 | /**
32 | * Converts the {@code JSValue} to java value.
33 | */
34 | public abstract T fromJSValue(JSContext context, JSValue value);
35 |
36 | /**
37 | * Returns a TypeAdapter equal to this TypeAdapter,
38 | * but with support for null java object and null/undefined javascript value.
39 | */
40 | public final TypeAdapter nullable() {
41 | return new NullableTypeAdapter<>(this);
42 | }
43 |
44 | private static class NullableTypeAdapter extends TypeAdapter {
45 |
46 | private final TypeAdapter delegate;
47 |
48 | NullableTypeAdapter(TypeAdapter delegate) {
49 | this.delegate = delegate;
50 | }
51 |
52 | @Override
53 | public JSValue toJSValue(JSContext context, T value) {
54 | if (value == null) return context.createJSNull();
55 | return delegate.toJSValue(context, value);
56 | }
57 |
58 | @Override
59 | public T fromJSValue(JSContext context, JSValue value) {
60 | if (value instanceof JSNull || value instanceof JSUndefined) return null;
61 | return delegate.fromJSValue(context, value);
62 | }
63 | }
64 |
65 | public interface Factory {
66 | @Nullable
67 | TypeAdapter> create(QuickJS quickJS, Type type);
68 | }
69 | }
70 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | plugins {
2 | id 'com.android.application'
3 | id 'org.jetbrains.kotlin.android'
4 | }
5 |
6 | android {
7 | namespace 'com.shiqi.testquickjs'
8 | compileSdk 33
9 |
10 | defaultConfig {
11 | applicationId "com.shiqi.testquickjs"
12 | minSdk 24
13 | targetSdk 33
14 | versionCode 1
15 | versionName "1.0"
16 |
17 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
18 | vectorDrawables {
19 | useSupportLibrary true
20 | }
21 | }
22 |
23 | buildTypes {
24 | release {
25 | minifyEnabled false
26 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
27 | }
28 | }
29 | compileOptions {
30 | sourceCompatibility JavaVersion.VERSION_1_8
31 | targetCompatibility JavaVersion.VERSION_1_8
32 | }
33 | kotlinOptions {
34 | jvmTarget = '1.8'
35 | }
36 | buildFeatures {
37 | compose true
38 | }
39 | composeOptions {
40 | kotlinCompilerExtensionVersion '1.4.5'
41 | }
42 | packagingOptions {
43 | resources {
44 | excludes += '/META-INF/{AL2.0,LGPL2.1}'
45 | }
46 | }
47 | }
48 |
49 | dependencies {
50 |
51 | implementation project(':quickjs-android')
52 | implementation("com.tencent.hippy:hippy-common:2.12.1")
53 |
54 | implementation 'androidx.core:core-ktx:1.9.0'
55 | implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1'
56 | implementation 'androidx.activity:activity-compose:1.5.1'
57 | implementation platform('androidx.compose:compose-bom:2022.10.00')
58 | implementation 'androidx.compose.ui:ui'
59 | implementation 'androidx.compose.ui:ui-graphics'
60 | implementation 'androidx.compose.ui:ui-tooling-preview'
61 | implementation 'androidx.compose.material3:material3'
62 | testImplementation 'junit:junit:4.13.2'
63 | androidTestImplementation 'androidx.test.ext:junit:1.1.5'
64 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
65 | androidTestImplementation platform('androidx.compose:compose-bom:2022.10.00')
66 | androidTestImplementation 'androidx.compose.ui:ui-test-junit4'
67 | debugImplementation 'androidx.compose.ui:ui-tooling'
68 | debugImplementation 'androidx.compose.ui:ui-test-manifest'
69 | }
--------------------------------------------------------------------------------
/quickjs/CMakeLists.txt:
--------------------------------------------------------------------------------
1 | cmake_minimum_required(VERSION 3.9.4)
2 | project(QUICKJS C)
3 |
4 | # Build type: "Debug" or "Release"
5 | if (CMAKE_BUILD_TYPE STREQUAL "Debug")
6 | set(COMPILE_FLAGS ${COMPILE_FLAGS} -g)
7 | elseif(CMAKE_BUILD_TYPE STREQUAL "Release")
8 | set(COMPILE_FLAGS ${COMPILE_FLAGS} -O3)
9 | endif()
10 |
11 | set(C_STANDARD 17)
12 | set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
13 | set(MI_OVERRIDE OFF)
14 |
15 | # interprocedural optimization (IPO/LTO)
16 | include(CheckIPOSupported)
17 | check_ipo_supported(RESULT supported OUTPUT error)
18 | if( supported AND NOT CMAKE_BUILD_TYPE STREQUAL "Debug")
19 | set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE)
20 | endif()
21 |
22 | set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
23 | set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)
24 |
25 | include_directories(${PROJECT_SOURCE_DIR})
26 | include_directories(${PROJECT_SOURCE_DIR}/include)
27 | include_directories(${PROJECT_SOURCE_DIR}/vendor/mimalloc/include)
28 | add_subdirectory(src)
29 | add_subdirectory(vendor/mimalloc)
30 |
31 | set(COMPILE_FLAGS -Wall -MMD -Wno-array-bounds -Wno-format-truncation)
32 |
33 | add_compile_options(${COMPILE_FLAGS})
34 | add_compile_definitions(
35 | _GNU_SOURCE
36 | CONFIG_BIGNUM
37 | CONFIG_VERSION=${QUICKJS_VERSION}
38 | )
39 |
40 | set(QJSC_CONFIG -DCONFIG_PREFIX="/usr/local" -DCONFIG_LTO)
41 | set(QJSC_EXE "${EXECUTABLE_OUTPUT_PATH}/qjsc")
42 | set(QJS_CONFIG ${QJSC_CONFIG} -DCONFIG_CC="clang")
43 |
44 | add_executable(qjsc qjsc.c quickjs-libc.c)
45 |
46 | # Add -lm if on Linux to handle math.h
47 | # See: https://askubuntu.com/a/332919
48 | if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
49 | list(APPEND LINK_LIBRARIES "m")
50 | endif()
51 |
52 | target_link_libraries(qjsc quickjs ${LINK_LIBRARIES} mimalloc-static)
53 | target_compile_definitions(qjsc PUBLIC ${QJSC_CONFIG})
54 |
55 | add_custom_command(
56 | TARGET qjsc POST_BUILD
57 | COMMAND ${QJSC_EXE} -c -o ${PROJECT_SOURCE_DIR}/repl.c -m ${PROJECT_SOURCE_DIR}/repl.js
58 | COMMAND ${QJSC_EXE} -fbignum -c -o ${PROJECT_SOURCE_DIR}/qjscalc.c ${PROJECT_SOURCE_DIR}/qjscalc.js
59 | )
60 |
61 | add_executable(qjs qjs.c quickjs-libc.c repl.c qjscalc.c)
62 | target_link_libraries(qjs quickjs ${LINK_LIBRARIES} mimalloc-static)
63 |
64 | add_executable(run-test262 run-test262.c quickjs-libc.c)
65 | target_link_libraries(run-test262 quickjs ${LINK_LIBRARIES} mimalloc-static)
66 |
--------------------------------------------------------------------------------
/quickjs-android/src/main/java/com/shiqi/quickjs/ArrayTypeAdapter.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2019 Hippo Seven
3 | * Copyright 2023-Present Shiqi Mei
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License");
6 | * you may not use this file except in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 |
18 | package com.shiqi.quickjs;
19 |
20 | import java.lang.reflect.Array;
21 | import java.lang.reflect.Type;
22 |
23 | class ArrayTypeAdapter extends TypeAdapter