Skip to content

Commit

Permalink
Quickjs (#5)
Browse files Browse the repository at this point in the history
* init optimization

* add json element support

* add JSParamters and JSResult

* segregate conversions

* reorg files

* remove js executor

* safe v8 + unit tests fix

* update export method to be more readable

* update settings.gradle

* Squashed 'quickjs/quickjs/' content from commit 3bb2ca3

git-subtree-dir: quickjs/quickjs
git-subtree-split: 3bb2ca360e994c93e9bf273a685c061d5e0b58f4

* init setup of jni

* draft conversion and memory management

* draft array, object, and function

* finalize Conversions.kt and JSContext.kt

* draft engine

* draft jni

* bug fix

* use delegation on jsobject and jsarray

* add support for json element

* json array bug fix

* add test for json array

* bug fix

* make JSConvertible also Releasable

* memory manager optimization

* add delegation for JSNull and JSUndefined

* implement JSFunction

* remove unnecessary code

* export function

* implement extend

* add console

* fix crash when atom hash returns 0

* clean up

* draft exporting class

* bug fix

* bug fix

* bug fix for JSClass function with parameters

* implement exporting property, replace java reflection with kotlin reflection

* bug fix

* bug fix for prototype instance method

* clean up JSFunctionBody

* memory leak fix

* revert changes made to quickjs.c

* add launch

* clean up

* make exception handler easier to use

* add completion closure to loadBundle

* fix jsCallback always return undefine instead of null for actual null value

* update env

* clean up

* Downgrade Gradle to get ./gradlew publishToSonatype to work

* Bump  version of io.github.gradle-nexus.publish-plugin to 2.0.0

* Adding github workflows for build, release, and snapshot.

* Uncommit the MainActivity.

* Remove unused old imports.

---------

Co-authored-by: Wenxi Zeng <[email protected]>
Co-authored-by: Wenxi Zeng <[email protected]>
  • Loading branch information
3 people authored Apr 14, 2024
1 parent 24c9330 commit 1d94da9
Show file tree
Hide file tree
Showing 106 changed files with 103,832 additions and 1,469 deletions.
39 changes: 39 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: CI

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
workflow_dispatch:

jobs:

cancel_previous:
permissions: write-all
runs-on: ubuntu-latest
steps:
- uses: styfle/[email protected]
with:
workflow_id: ${{ github.event.workflow.id }}

build:

needs: cancel_previous
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: Grant execute permission for gradlew
run: chmod +x gradlew
- name: cache gradle dependencies
uses: actions/cache@v2
with:
path: |
~/.gradle/caches
~/.gradle/wrapper
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
restore-keys: |
${{ runner.os }}-gradle-
- name: Run Tests
run: ./gradlew check
55 changes: 55 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
name: Release

on:
push:
tags:
- '*.*.*'

jobs:
release:
permissions: write-all
runs-on: ubuntu-latest
environment: deployment
steps:
- uses: actions/checkout@v2
- name: Get tag
id: vars
run: echo ::set-output name=tag::${GITHUB_REF#refs/*/}
- name: Verify tag
run: |
VERSION=$(grep VERSION_NAME gradle.properties | awk -F= '{ print $2 }' | sed "s/-SNAPSHOT//")
if [ "${{ steps.vars.outputs.tag }}" != "$VERSION" ]; then {
echo "Tag ${{ steps.vars.outputs.tag }} does not match the package version ($VERSION)"
exit 1
} fi
- name: Grant execute permission for gradlew
run: chmod +x gradlew
- name: cache gradle dependencies
uses: actions/cache@v2
with:
path: |
~/.gradle/caches
~/.gradle/wrapper
key: ${{ runner.os }}-gradle-core-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
restore-keys: |
${{ runner.os }}-gradle-core-
- name: Publush release to sonatype
run: ./gradlew publishToSonatype -Prelease closeAndReleaseSonatypeStagingRepository
env:
SIGNING_KEY_ID: ${{ secrets.SIGNING_KEY_ID }}
SIGNING_KEY_PASSWORD: ${{ secrets.SIGNING_KEY_PASSWORD }}
ORG_GRADLE_PROJECT_sonatypeUsername: ${{ secrets.NEXUS_USERNAME }}
ORG_GRADLE_PROJECT_sonatypePassword: ${{ secrets.NEXUS_PASSWORD }}
SIGNING_PRIVATE_KEY_BASE64: ${{ secrets.SIGNING_PRIVATE_KEY_BASE64 }}

- name: create release
run: |
curl \
-X POST \
-H "Authorization: token $GITHUB_TOKEN" \
https://api.github.com/repos/${{github.repository}}/releases \
-d '{"tag_name": "${{ env.RELEASE_VERSION }}", "name": "${{ env.RELEASE_VERSION }}", "body": "Release of version ${{ env.RELEASE_VERSION }}", "draft": false, "prerelease": false, "generate_release_notes": true}'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
RELEASE_VERSION: ${{ steps.vars.outputs.tag }}
31 changes: 31 additions & 0 deletions .github/workflows/snapshot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Snapshot

on:
push:
branches: [ main ]

jobs:
snapshot:
runs-on: ubuntu-latest
environment: deployment
steps:
- uses: actions/checkout@v2
- name: Grant execute permission for gradlew
run: chmod +x gradlew
- name: cache gradle dependencies
uses: actions/cache@v2
with:
path: |
~/.gradle/caches
~/.gradle/wrapper
key: ${{ runner.os }}-gradle-core-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
restore-keys: |
${{ runner.os }}-gradle-core-
- name: Publush snapshot to sonatype
run: ./gradlew publishToSonatype
env:
SIGNING_KEY_ID: ${{ secrets.SIGNING_KEY_ID }}
SIGNING_KEY_PASSWORD: ${{ secrets.SIGNING_KEY_PASSWORD }}
ORG_GRADLE_PROJECT_sonatypeUsername: ${{ secrets.NEXUS_USERNAME }}
ORG_GRADLE_PROJECT_sonatypePassword: ${{ secrets.NEXUS_PASSWORD }}
SIGNING_PRIVATE_KEY_BASE64: ${{ secrets.SIGNING_PRIVATE_KEY_BASE64 }}
6 changes: 3 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ plugins {
}

android {
compileSdk 31
compileSdk 34

defaultConfig {
applicationId "com.segment.analytics.substrata.kotlin.sample"
minSdk 21
targetSdk 31
targetSdk 34
versionCode 1
versionName "1.0"

Expand All @@ -29,12 +29,12 @@ android {
kotlinOptions {
jvmTarget = '1.8'
}
namespace 'com.segment.analytics.substrata.kotlin.sample'
}

dependencies {
// api 'com.segment.analytics.kotlin:substrata-kotlin:0.0.2'
api project(':substrata-kotlin')
implementation 'com.eclipsesource.j2v8:j2v8:6.2.0@aar'
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'com.google.android.material:material:1.5.0'
Expand Down
3 changes: 1 addition & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.segment.analytics.substrata.kotlin.sample">
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

<!-- Required for internet. -->
<uses-permission android:name="android.permission.INTERNET" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,101 +3,88 @@ package com.segment.analytics.substrata.kotlin.sample
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import com.segment.analytics.substrata.kotlin.JSValue
import com.segment.analytics.substrata.kotlin.j2v8.J2V8Engine

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
Log.d("PRAY", "Starting")
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val engine = J2V8Engine()
Log.d("PRAY", "Created engine")
engine["foo"] = JSValue.JSString("")
val script1 = applicationContext.assets.open("sample.js")
val script2 = applicationContext.assets.open("sample2.js").bufferedReader().readText()
engine.bridge["foo"] = JSValue.JSString("bar")
engine.loadBundle(script1) { }
val res = engine.execute(
"""
function barFoo() {
return "Foo";
}
const foo = {x: "Test"};
const bar = [10, 2, "foo", true];
console.log("123");
console.log(1);
console.log(2.9);
console.log(true);
console.log(foo);
console.log(bar);
console.log(DataBridge["foo"]);
foo.y = 2.8;
foo
""".trimIndent()
)

Log.d("PRAY", res.toString())
val res2 = engine.call("fooBar")
Log.d("PRAY", res2.toString())
val res3 = engine.call("barFoo")
Log.d("PRAY", res3.toString())
engine.execute(script2)
// engine.exec(
// val engine = J2V8Engine()
// Log.d("PRAY", "Created engine")
// engine["foo"] = JSValue.JSString("")
// val script1 = applicationContext.assets.open("sample.js")
// val script2 = applicationContext.assets.open("sample2.js").bufferedReader().readText()
// engine.bridge["foo"] = JSValue.JSString("bar")
// engine.loadBundle(script1) { }
// val res = engine.execute(
// """
// pray
// """.trimIndent() // ReferenceError
// )
// engine.exec(
// """
// pray()
// """.trimIndent() // ReferenceError
// )
// engine.exec(
// """
// throw "pray-error"
// """.trimIndent() // String "pray-error"
// )
// engine.exec(
// """
// throw 'Parameter is not a number!';
// """.trimIndent() // String "pray-error"
// )
// engine.exec(
// """
// function
// """.trimIndent() // SyntaxError
// )
// engine.exec(
// """
// var x = ""
// var x = 2
// function barFoo() {
// return "Foo";
// }
// const foo = {x: "Test"};
// const bar = [10, 2, "foo", true];
// console.log("123");
// console.log(1);
// console.log(2.9);
// console.log(true);
// console.log(foo);
// console.log(bar);
// console.log(DataBridge["foo"]);
// foo.y = 2.8;
// foo
//
// """.trimIndent()
// )
// engine.exec(
// """
// const y = ""
// const y = 2
// """.trimIndent() // SyntaxError
// )
// try {
// engine.call("playa")
// } catch (ex: Exception) {
// ex.printStackTrace()
// Log.d("PRAY", ex.toString())
// }

}

fun J2V8Engine.exec(script: String) {
Log.d("PRAY-errorHandler", "Execing $script")
try {
execute(script)
} catch (ex: Exception) {
ex.printStackTrace()
}
Log.d("PRAY-errorHandler", "DONE Execing $script")
//
// Log.d("PRAY", res.toString())
// val res2 = engine.call("fooBar")
// Log.d("PRAY", res2.toString())
// val res3 = engine.call("barFoo")
// Log.d("PRAY", res3.toString())
// engine.execute(script2)
//// engine.exec(
//// """
//// pray
//// """.trimIndent() // ReferenceError
//// )
//// engine.exec(
//// """
//// pray()
//// """.trimIndent() // ReferenceError
//// )
//// engine.exec(
//// """
//// throw "pray-error"
//// """.trimIndent() // String "pray-error"
//// )
//// engine.exec(
//// """
//// throw 'Parameter is not a number!';
//// """.trimIndent() // String "pray-error"
//// )
//// engine.exec(
//// """
//// function
//// """.trimIndent() // SyntaxError
//// )
//// engine.exec(
//// """
//// var x = ""
//// var x = 2
//// """.trimIndent()
//// )
//// engine.exec(
//// """
//// const y = ""
//// const y = 2
//// """.trimIndent() // SyntaxError
//// )
//// try {
//// engine.call("playa")
//// } catch (ex: Exception) {
//// ex.printStackTrace()
//// Log.d("PRAY", ex.toString())
//// }
}
}
8 changes: 4 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ buildscript {
}

plugins {
id 'com.android.application' version '7.1.2' apply false
id 'com.android.library' version '7.1.2' apply false
id 'org.jetbrains.kotlin.android' version '1.6.10' apply false
id 'io.github.gradle-nexus.publish-plugin' version "1.1.0"
id 'com.android.application' version '7.4.2' apply false
id 'com.android.library' version '7.4.2' apply false
id 'org.jetbrains.kotlin.android' version '1.6.21' apply false
id 'io.github.gradle-nexus.publish-plugin' version "2.0.0"
}

task clean(type: Delete) {
Expand Down
8 changes: 5 additions & 3 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ android.nonTransitiveRClass=true

GROUP=com.segment.analytics.kotlin

VERSION_CODE=006
VERSION_NAME=0.0.6
VERSION_CODE=008
VERSION_NAME=0.0.8

POM_NAME=Substrata
POM_DESCRIPTION=The hassle-free way to add analytics to your Kotlin app.
Expand All @@ -43,4 +43,6 @@ POM_LICENCE_URL=http://opensource.org/licenses/MIT
POM_LICENCE_DIST=repo

POM_DEVELOPER_ID=segmentio
POM_DEVELOPER_NAME=Segment, Inc.
POM_DEVELOPER_NAME=Segment, Inc.
android.defaults.buildfeatures.buildconfig=true
android.nonFinalResIds=false
6 changes: 3 additions & 3 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Wed Apr 06 14:54:34 PDT 2022
#Thu Apr 11 17:16:54 EDT 2024
distributionBase=GRADLE_USER_HOME
distributionUrl=https\://services.gradle.org/distributions/gradle-7.2-bin.zip
distributionPath=wrapper/dists
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.6.2-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Loading

0 comments on commit 1d94da9

Please sign in to comment.