Skip to content

Commit b310449

Browse files
author
梅世祺
committed
Initial Commit
0 parents  commit b310449

File tree

309 files changed

+142711
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

309 files changed

+142711
-0
lines changed

.gitignore

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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/

.gitmodules

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[submodule "quickjs/vendor/mimalloc"]
2+
path = quickjs/vendor/mimalloc
3+
url = https://github.com/microsoft/mimalloc.git
4+
branch = v1.7.9

app/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

app/CMakeLists.txt

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
target_compile_options(quickjs-android PRIVATE ${COMMON_FLAGS})
29+
target_link_libraries(quickjs-android PRIVATE quickjs mimalloc)

app/build.gradle

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
plugins {
2+
id 'com.android.application'
3+
id 'org.jetbrains.kotlin.android'
4+
}
5+
6+
android {
7+
namespace 'com.kugou.quickjs'
8+
compileSdk 33
9+
10+
defaultConfig {
11+
applicationId "com.kugou.quickjs"
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+
// build quickjs
23+
externalNativeBuild {
24+
cmake {
25+
targets 'quickjs-android'
26+
arguments '-DCMAKE_VERBOSE_MAKEFILE:BOOL=ON'
27+
}
28+
}
29+
}
30+
31+
externalNativeBuild {
32+
cmake {
33+
path 'CMakeLists.txt'
34+
}
35+
}
36+
37+
buildTypes {
38+
release {
39+
minifyEnabled false
40+
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
41+
}
42+
}
43+
compileOptions {
44+
sourceCompatibility JavaVersion.VERSION_1_8
45+
targetCompatibility JavaVersion.VERSION_1_8
46+
}
47+
kotlinOptions {
48+
jvmTarget = '1.8'
49+
}
50+
buildFeatures {
51+
compose true
52+
}
53+
composeOptions {
54+
kotlinCompilerExtensionVersion '1.4.5'
55+
}
56+
packagingOptions {
57+
resources {
58+
excludes += '/META-INF/{AL2.0,LGPL2.1}'
59+
}
60+
}
61+
}
62+
63+
dependencies {
64+
65+
implementation 'androidx.core:core-ktx:1.10.1'
66+
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.6.1'
67+
implementation 'androidx.activity:activity-compose:1.7.2'
68+
implementation platform('androidx.compose:compose-bom:2022.10.00')
69+
implementation 'androidx.compose.ui:ui'
70+
implementation 'androidx.compose.ui:ui-graphics'
71+
implementation 'androidx.compose.ui:ui-tooling-preview'
72+
implementation 'androidx.compose.material3:material3'
73+
testImplementation 'junit:junit:4.13.2'
74+
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
75+
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
76+
androidTestImplementation platform('androidx.compose:compose-bom:2022.10.00')
77+
androidTestImplementation 'androidx.compose.ui:ui-test-junit4'
78+
debugImplementation 'androidx.compose.ui:ui-tooling'
79+
debugImplementation 'androidx.compose.ui:ui-test-manifest'
80+
}

app/proguard-rules.pro

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.kugou.quickjs
2+
3+
import androidx.test.platform.app.InstrumentationRegistry
4+
import androidx.test.ext.junit.runners.AndroidJUnit4
5+
6+
import org.junit.Test
7+
import org.junit.runner.RunWith
8+
9+
import org.junit.Assert.*
10+
11+
/**
12+
* Instrumented test, which will execute on an Android device.
13+
*
14+
* See [testing documentation](http://d.android.com/tools/testing).
15+
*/
16+
@RunWith(AndroidJUnit4::class)
17+
class ExampleInstrumentedTest {
18+
@Test
19+
fun useAppContext() {
20+
// Context of the app under test.
21+
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
22+
assertEquals("com.kugou.quickjs", appContext.packageName)
23+
}
24+
}

app/src/main/AndroidManifest.xml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:tools="http://schemas.android.com/tools">
4+
5+
<application
6+
android:allowBackup="true"
7+
android:dataExtractionRules="@xml/data_extraction_rules"
8+
android:fullBackupContent="@xml/backup_rules"
9+
android:icon="@mipmap/ic_launcher"
10+
android:label="@string/app_name"
11+
android:roundIcon="@mipmap/ic_launcher_round"
12+
android:supportsRtl="true"
13+
android:theme="@style/Theme.QuickJS"
14+
tools:targetApi="31">
15+
<activity
16+
android:name=".MainActivity"
17+
android:exported="true"
18+
android:label="@string/app_name"
19+
android:theme="@style/Theme.QuickJS">
20+
<intent-filter>
21+
<action android:name="android.intent.action.MAIN" />
22+
23+
<category android:name="android.intent.category.LAUNCHER" />
24+
</intent-filter>
25+
</activity>
26+
</application>
27+
28+
</manifest>

app/src/main/c/java-helper.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include <stdio.h>
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+
}

app/src/main/c/java-helper.h

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#ifndef QUICKJS_ANDROID_JAVA_HELPER_H
2+
#define QUICKJS_ANDROID_JAVA_HELPER_H
3+
4+
#include <jni.h>
5+
6+
#define CLASS_NAME_ILLEGAL_STATE_EXCEPTION "java/lang/IllegalStateException"
7+
#define CLASS_NAME_JS_DATA_EXCEPTION "com/shiqi/quickjs/JSDataException"
8+
9+
#define THROW_EXCEPTION(ENV, EXCEPTION_NAME, ...) \
10+
do { \
11+
throw_exception((ENV), (EXCEPTION_NAME), __VA_ARGS__); \
12+
return; \
13+
} while (0)
14+
15+
#define THROW_EXCEPTION_RET(ENV, EXCEPTION_NAME, ...) \
16+
do { \
17+
throw_exception((ENV), (EXCEPTION_NAME), __VA_ARGS__); \
18+
return 0; \
19+
} while (0)
20+
21+
#define THROW_ILLEGAL_STATE_EXCEPTION(ENV, ...) \
22+
THROW_EXCEPTION(ENV, CLASS_NAME_ILLEGAL_STATE_EXCEPTION, __VA_ARGS__)
23+
24+
#define THROW_ILLEGAL_STATE_EXCEPTION_RET(ENV, ...) \
25+
THROW_EXCEPTION_RET(ENV, CLASS_NAME_ILLEGAL_STATE_EXCEPTION, __VA_ARGS__)
26+
27+
#define THROW_JS_DATA_EXCEPTION(ENV, ...) \
28+
THROW_EXCEPTION(ENV, CLASS_NAME_JS_DATA_EXCEPTION, __VA_ARGS__)
29+
30+
#define THROW_JS_DATA_EXCEPTION_RET(ENV, ...) \
31+
THROW_EXCEPTION_RET(ENV, CLASS_NAME_JS_DATA_EXCEPTION, __VA_ARGS__)
32+
33+
#define CHECK_NULL(ENV, POINTER, MESSAGE) \
34+
do { \
35+
if ((POINTER) == NULL) { \
36+
THROW_ILLEGAL_STATE_EXCEPTION((ENV), (MESSAGE)); \
37+
} \
38+
} while (0)
39+
40+
#define CHECK_NULL_RET(ENV, POINTER, MESSAGE) \
41+
do { \
42+
if ((POINTER) == NULL) { \
43+
THROW_ILLEGAL_STATE_EXCEPTION_RET((ENV), (MESSAGE)); \
44+
} \
45+
} while (0)
46+
47+
#define CHECK_FALSE_RET(ENV, STATEMENT, MESSAGE) \
48+
do { \
49+
if (!(STATEMENT)) { \
50+
THROW_ILLEGAL_STATE_EXCEPTION_RET((ENV), (MESSAGE)); \
51+
} \
52+
} while (0)
53+
54+
jint throw_exception(JNIEnv *env, const char *exception_name, const char *message, ...);
55+
56+
#define OBTAIN_ENV(VM) \
57+
JNIEnv *env = NULL; \
58+
int __require_detach__ = 0; \
59+
do { \
60+
(*(VM))->GetEnv((VM), (void **) &env, JNI_VERSION_1_6); \
61+
if (env == NULL) __require_detach__ = (*(VM))->AttachCurrentThread((VM), &env, NULL) == JNI_OK; \
62+
} while (0)
63+
64+
#define RELEASE_ENV(VM) \
65+
do { \
66+
if (__require_detach__) (*(VM))->DetachCurrentThread((VM)); \
67+
} while (0)
68+
69+
#endif //QUICKJS_ANDROID_JAVA_HELPER_H

0 commit comments

Comments
 (0)