Skip to content

Commit ac8da8f

Browse files
committed
quickjs-for-quickjs
1 parent 102d7c0 commit ac8da8f

File tree

10 files changed

+160
-2
lines changed

10 files changed

+160
-2
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License
2+
3+
quickjs-emscripten copyright (c) 2019-2024 Jake Teton-Landis
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# quickjs-for-quickjs
2+
3+
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.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import fs from "node:fs/promises"
2+
import module from "node:module"
3+
const require = module.createRequire(import.meta.url)
4+
5+
const quickjsSource = await fs.readFile(require.resolve("quickjs-for-quickjs"), "utf8")
6+
const exportQuickjsSource = `export default ${JSON.stringify(quickjsSource)}`
7+
8+
const exampleQuickjsHostSource = await fs.readFile(require.resolve("./quickjs-host.mjs"), "utf8")
9+
const exportExampleQuickjsHostSource = `export default ${JSON.stringify(exampleQuickjsHostSource)}`
10+
11+
const QuickJS = await import("quickjs-emscripten").then((mod) => mod.getQuickJS())
12+
const context = QuickJS.newContext()
13+
context.runtime.setModuleLoader((name) => {
14+
if (name === "quickjs-for-quickjs") {
15+
return quickjsSource
16+
}
17+
if (name === "quickjs-for-quickjs-source") {
18+
return exportQuickjsSource
19+
}
20+
if (name === "example-quickjs-host-source") {
21+
return exportExampleQuickjsHostSource
22+
}
23+
return { error: new Error("not found") }
24+
})
25+
context.setProp(
26+
context.global,
27+
"random",
28+
context.newFunction("random", () => context.newNumber(Math.random())),
29+
)
30+
const promise = context.resolvePromise(context.evalCode(exampleQuickjsHostSource).unwrap())
31+
context.runtime.executePendingJobs()
32+
console.log("result:", context.dump(await promise))
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import quickjsSource from "quickjs-for-quickjs-source"
2+
import exampleQuickjsHostSource from "example-quickjs-host-source"
3+
const QuickJS = await import("quickjs-for-quickjs").then((mod) => mod.getQuickJS())
4+
const exportQuickjsSource = `export default ${JSON.stringify(quickjsSource)}`
5+
const exportExampleQuickjsHostSource = `export default ${JSON.stringify(exampleQuickjsHostSource)}`
6+
7+
const context = QuickJS.newContext()
8+
context.runtime.setModuleLoader((name) => {
9+
if (name === "quickjs-for-quickjs") {
10+
return quickjsSource
11+
}
12+
if (name === "quickjs-for-quickjs-source") {
13+
return exportQuickjsSource
14+
}
15+
if (name === "example-quickjs-host-source") {
16+
return exportExampleQuickjsHostSource
17+
}
18+
return { error: new Error("not found") }
19+
})
20+
context.setProp(
21+
context.global,
22+
"random",
23+
context.newFunction("random", () => context.newNumber(random())),
24+
)
25+
26+
const codeToEval =
27+
random() > 0.5 ? `export const result = ['hello', 'world'].join(' ')` : exampleQuickjsHostSource
28+
const promise = context.resolvePromise(context.evalCode(codeToEval).unwrap())
29+
context.runtime.executePendingJobs()
30+
export const result = "hello " + context.dump(await promise)
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{
2+
"name": "quickjs-for-quickjs",
3+
"version": "0.30.0",
4+
"license": "MIT",
5+
"repository": {
6+
"type": "git",
7+
"url": "https://github.com/justjake/quickjs-emscripten"
8+
},
9+
"author": {
10+
"name": "Jake Teton-Landis",
11+
"url": "https://jake.tl"
12+
},
13+
"scripts": {
14+
"check": "npx tsc --project . --noEmit",
15+
"build": "npx tsup",
16+
"clean": "git clean -fx dist"
17+
},
18+
"files": [
19+
"LICENSE",
20+
"example/**",
21+
"dist/**/*"
22+
],
23+
"types": "dist/index.d.mts",
24+
"main": "dist/index.mjs",
25+
"module": "dist/index.mjs",
26+
"exports": {
27+
"./package.json": "./package.json",
28+
".": {
29+
"types": "./dist/index.d.mts",
30+
"import": "./dist/index.mjs",
31+
"default": "./dist/index.mjs"
32+
}
33+
},
34+
"devDependencies": {
35+
"quickjs-emscripten-core": "workspace:*",
36+
"@jitl/quickjs-asmjs-mjs-release-sync": "workspace:*",
37+
"@jitl/tsconfig": "workspace:*"
38+
}
39+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import type { QuickJSWASMModule } from "quickjs-emscripten-core"
2+
import { newQuickJSWASMModuleFromVariant } from "quickjs-emscripten-core"
3+
import RELEASE_SYNC from "@jitl/quickjs-asmjs-mjs-release-sync"
4+
5+
export * from "quickjs-emscripten-core"
6+
7+
let promise: Promise<QuickJSWASMModule> | undefined
8+
9+
export function getQuickJS(): Promise<QuickJSWASMModule> {
10+
return (promise ??= newQuickJSWASMModule())
11+
}
12+
13+
export function newQuickJSWASMModule(): Promise<QuickJSWASMModule> {
14+
// TODO: why are the types mad?
15+
return newQuickJSWASMModuleFromVariant(RELEASE_SYNC as any)
16+
}
17+
18+
export { RELEASE_SYNC }
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"extends": "@jitl/tsconfig/tsconfig.json",
3+
"include": ["src/**/*"],
4+
"compilerOptions": {
5+
"rootDir": "src"
6+
}
7+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { extendConfig } from "@jitl/tsconfig/tsup.base.config.js"
2+
3+
export default extendConfig({
4+
entry: ["src/index.mts"],
5+
format: ["esm"],
6+
external: [],
7+
clean: true,
8+
})

packages/variant-quickjs-asmjs-mjs-release-sync/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { QuickJSSyncVariant } from "@jitl/quickjs-ffi-types"
2-
import { QuickJSFFI } from "./ffi.js"
32
import moduleLoader from "@jitl/quickjs-asmjs-mjs-release-sync/emscripten-module"
3+
import { QuickJSFFI } from "./ffi.js"
44
/**
55
* ### @jitl/quickjs-asmjs-mjs-release-sync
66
*

scripts/prepareVariants.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -760,8 +760,8 @@ function renderIndexTs(
760760
// Eager loading please!
761761
return `
762762
import type { ${variantTypeName} } from '@jitl/quickjs-ffi-types'
763-
import { ${className} } from './ffi.js'
764763
import moduleLoader from '${packageJson.name}/emscripten-module'
764+
import { ${className} } from './ffi.js'
765765
/**
766766
${docComment}
767767
*/

0 commit comments

Comments
 (0)