Skip to content

Commit

Permalink
quickjs-for-quickjs
Browse files Browse the repository at this point in the history
  • Loading branch information
justjake committed Sep 1, 2024
1 parent 102d7c0 commit ac8da8f
Show file tree
Hide file tree
Showing 10 changed files with 160 additions and 2 deletions.
21 changes: 21 additions & 0 deletions packages/quickjs-for-quickjs/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License

quickjs-emscripten copyright (c) 2019-2024 Jake Teton-Landis

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
3 changes: 3 additions & 0 deletions packages/quickjs-for-quickjs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# quickjs-for-quickjs

This package is a build of [quickjs-emscripten](https://github.com/justjake/quickjs-emscripten) that can run inside QuickJS or any other JavaScript runtime without WebAssembly support. The QuickJS C library is compiled to Asm.js, and then bundled together with the quickjs-emscripten JavaScript wrapper into a single standalone file with no external dependencies.
32 changes: 32 additions & 0 deletions packages/quickjs-for-quickjs/example/node-host.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import fs from "node:fs/promises"
import module from "node:module"
const require = module.createRequire(import.meta.url)

const quickjsSource = await fs.readFile(require.resolve("quickjs-for-quickjs"), "utf8")
const exportQuickjsSource = `export default ${JSON.stringify(quickjsSource)}`

const exampleQuickjsHostSource = await fs.readFile(require.resolve("./quickjs-host.mjs"), "utf8")
const exportExampleQuickjsHostSource = `export default ${JSON.stringify(exampleQuickjsHostSource)}`

const QuickJS = await import("quickjs-emscripten").then((mod) => mod.getQuickJS())
const context = QuickJS.newContext()
context.runtime.setModuleLoader((name) => {
if (name === "quickjs-for-quickjs") {
return quickjsSource
}
if (name === "quickjs-for-quickjs-source") {
return exportQuickjsSource
}
if (name === "example-quickjs-host-source") {
return exportExampleQuickjsHostSource
}
return { error: new Error("not found") }
})
context.setProp(
context.global,
"random",
context.newFunction("random", () => context.newNumber(Math.random())),
)
const promise = context.resolvePromise(context.evalCode(exampleQuickjsHostSource).unwrap())
context.runtime.executePendingJobs()
console.log("result:", context.dump(await promise))
30 changes: 30 additions & 0 deletions packages/quickjs-for-quickjs/example/quickjs-host.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import quickjsSource from "quickjs-for-quickjs-source"
import exampleQuickjsHostSource from "example-quickjs-host-source"
const QuickJS = await import("quickjs-for-quickjs").then((mod) => mod.getQuickJS())
const exportQuickjsSource = `export default ${JSON.stringify(quickjsSource)}`
const exportExampleQuickjsHostSource = `export default ${JSON.stringify(exampleQuickjsHostSource)}`

const context = QuickJS.newContext()
context.runtime.setModuleLoader((name) => {
if (name === "quickjs-for-quickjs") {
return quickjsSource
}
if (name === "quickjs-for-quickjs-source") {
return exportQuickjsSource
}
if (name === "example-quickjs-host-source") {
return exportExampleQuickjsHostSource
}
return { error: new Error("not found") }
})
context.setProp(
context.global,
"random",
context.newFunction("random", () => context.newNumber(random())),
)

const codeToEval =
random() > 0.5 ? `export const result = ['hello', 'world'].join(' ')` : exampleQuickjsHostSource
const promise = context.resolvePromise(context.evalCode(codeToEval).unwrap())
context.runtime.executePendingJobs()
export const result = "hello " + context.dump(await promise)
39 changes: 39 additions & 0 deletions packages/quickjs-for-quickjs/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"name": "quickjs-for-quickjs",
"version": "0.30.0",
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/justjake/quickjs-emscripten"
},
"author": {
"name": "Jake Teton-Landis",
"url": "https://jake.tl"
},
"scripts": {
"check": "npx tsc --project . --noEmit",
"build": "npx tsup",
"clean": "git clean -fx dist"
},
"files": [
"LICENSE",
"example/**",
"dist/**/*"
],
"types": "dist/index.d.mts",
"main": "dist/index.mjs",
"module": "dist/index.mjs",
"exports": {
"./package.json": "./package.json",
".": {
"types": "./dist/index.d.mts",
"import": "./dist/index.mjs",
"default": "./dist/index.mjs"
}
},
"devDependencies": {
"quickjs-emscripten-core": "workspace:*",
"@jitl/quickjs-asmjs-mjs-release-sync": "workspace:*",
"@jitl/tsconfig": "workspace:*"
}
}
18 changes: 18 additions & 0 deletions packages/quickjs-for-quickjs/src/index.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import type { QuickJSWASMModule } from "quickjs-emscripten-core"
import { newQuickJSWASMModuleFromVariant } from "quickjs-emscripten-core"
import RELEASE_SYNC from "@jitl/quickjs-asmjs-mjs-release-sync"

export * from "quickjs-emscripten-core"

let promise: Promise<QuickJSWASMModule> | undefined

export function getQuickJS(): Promise<QuickJSWASMModule> {
return (promise ??= newQuickJSWASMModule())
}

export function newQuickJSWASMModule(): Promise<QuickJSWASMModule> {
// TODO: why are the types mad?
return newQuickJSWASMModuleFromVariant(RELEASE_SYNC as any)
}

export { RELEASE_SYNC }
7 changes: 7 additions & 0 deletions packages/quickjs-for-quickjs/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "@jitl/tsconfig/tsconfig.json",
"include": ["src/**/*"],
"compilerOptions": {
"rootDir": "src"
}
}
8 changes: 8 additions & 0 deletions packages/quickjs-for-quickjs/tsup.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { extendConfig } from "@jitl/tsconfig/tsup.base.config.js"

export default extendConfig({
entry: ["src/index.mts"],
format: ["esm"],
external: [],
clean: true,
})
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { QuickJSSyncVariant } from "@jitl/quickjs-ffi-types"
import { QuickJSFFI } from "./ffi.js"
import moduleLoader from "@jitl/quickjs-asmjs-mjs-release-sync/emscripten-module"
import { QuickJSFFI } from "./ffi.js"
/**
* ### @jitl/quickjs-asmjs-mjs-release-sync
*
Expand Down
2 changes: 1 addition & 1 deletion scripts/prepareVariants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -760,8 +760,8 @@ function renderIndexTs(
// Eager loading please!
return `
import type { ${variantTypeName} } from '@jitl/quickjs-ffi-types'
import { ${className} } from './ffi.js'
import moduleLoader from '${packageJson.name}/emscripten-module'
import { ${className} } from './ffi.js'
/**
${docComment}
*/
Expand Down

0 comments on commit ac8da8f

Please sign in to comment.