@componentor/quickjs-for-quickjs 0.31.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +70 -0
- package/dist/index.d.mts +10 -0
- package/dist/index.mjs +6 -0
- package/example/node-host.mjs +51 -0
- package/example/quickjs-host.mjs +49 -0
- package/package.json +39 -0
- package/yodawg.jpg +0 -0
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import fs from "node:fs/promises"
|
|
2
|
+
import module from "node:module"
|
|
3
|
+
import { getQuickJS, setUpContext } from "quickjs-for-quickjs"
|
|
4
|
+
const require = module.createRequire(import.meta.url)
|
|
5
|
+
|
|
6
|
+
console.log("hello")
|
|
7
|
+
|
|
8
|
+
const quickjsSource = await fs.readFile(require.resolve("quickjs-for-quickjs"), "utf8")
|
|
9
|
+
const exportQuickjsSource = `export default ${JSON.stringify(quickjsSource)}`
|
|
10
|
+
|
|
11
|
+
const exampleQuickjsHostSource = await fs.readFile(require.resolve("./quickjs-host.mjs"), "utf8")
|
|
12
|
+
const exportExampleQuickjsHostSource = `export default ${JSON.stringify(exampleQuickjsHostSource)}`
|
|
13
|
+
|
|
14
|
+
const QuickJS = await getQuickJS()
|
|
15
|
+
const context = setUpContext(QuickJS.newContext())
|
|
16
|
+
context.runtime.setModuleLoader((name) => {
|
|
17
|
+
console.log(`0: import ${name}`)
|
|
18
|
+
if (name === "quickjs-for-quickjs") {
|
|
19
|
+
return quickjsSource
|
|
20
|
+
}
|
|
21
|
+
if (name === "quickjs-for-quickjs-source") {
|
|
22
|
+
return exportQuickjsSource
|
|
23
|
+
}
|
|
24
|
+
if (name === "example-quickjs-host-source") {
|
|
25
|
+
return exportExampleQuickjsHostSource
|
|
26
|
+
}
|
|
27
|
+
return { error: new Error("not found") }
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
let nestingLimit = 2
|
|
31
|
+
context.setProp(
|
|
32
|
+
context.global,
|
|
33
|
+
"done",
|
|
34
|
+
context.newFunction("done", () => context.newNumber(--nestingLimit <= 0 ? 1 : 0)),
|
|
35
|
+
)
|
|
36
|
+
const handle = context
|
|
37
|
+
.evalCode(exampleQuickjsHostSource, "quickjs-host.mjs", {
|
|
38
|
+
type: "module",
|
|
39
|
+
})
|
|
40
|
+
.unwrap()
|
|
41
|
+
|
|
42
|
+
const promise = context.resolvePromise(handle)
|
|
43
|
+
context.runtime.executePendingJobs()
|
|
44
|
+
|
|
45
|
+
const result = (await promise).unwrap()
|
|
46
|
+
|
|
47
|
+
console.log({
|
|
48
|
+
result: context.dump(context.getProp(result, "result")),
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
console.log("result:", context.dump(context.getProp(await promise, "result")))
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { getQuickJS, setUpContext } from "quickjs-for-quickjs"
|
|
2
|
+
import quickjsSource from "quickjs-for-quickjs-source"
|
|
3
|
+
import exampleQuickjsHostSource from "example-quickjs-host-source"
|
|
4
|
+
|
|
5
|
+
console.log(`${console.depth}: started`)
|
|
6
|
+
const QuickJS = await getQuickJS()
|
|
7
|
+
|
|
8
|
+
const exportQuickjsSource = `export default ${JSON.stringify(quickjsSource)}`
|
|
9
|
+
const exportExampleQuickjsHostSource = `export default ${JSON.stringify(exampleQuickjsHostSource)}`
|
|
10
|
+
|
|
11
|
+
console.log(`${console.depth}: imported`)
|
|
12
|
+
|
|
13
|
+
const context = setUpContext(QuickJS.newContext())
|
|
14
|
+
context.runtime.setModuleLoader((name) => {
|
|
15
|
+
console.log(`${console.depth}: import ${name}`)
|
|
16
|
+
if (name === "quickjs-for-quickjs") {
|
|
17
|
+
return quickjsSource
|
|
18
|
+
}
|
|
19
|
+
if (name === "quickjs-for-quickjs-source") {
|
|
20
|
+
return exportQuickjsSource
|
|
21
|
+
}
|
|
22
|
+
if (name === "example-quickjs-host-source") {
|
|
23
|
+
return exportExampleQuickjsHostSource
|
|
24
|
+
}
|
|
25
|
+
return { error: new Error("not found") }
|
|
26
|
+
})
|
|
27
|
+
context.setProp(
|
|
28
|
+
context.global,
|
|
29
|
+
"done",
|
|
30
|
+
context.newFunction("random", () => context.newNumber(done())),
|
|
31
|
+
)
|
|
32
|
+
|
|
33
|
+
console.log(`${console.depth}: evaluating`)
|
|
34
|
+
|
|
35
|
+
const codeToEval = done()
|
|
36
|
+
? `export const result = ['hello', 'world', console.depth].join(' ')`
|
|
37
|
+
: exampleQuickjsHostSource
|
|
38
|
+
const promise = context.resolvePromise(context.evalCode(codeToEval).unwrap())
|
|
39
|
+
|
|
40
|
+
console.log(`${console.depth}: executing pending jobs`)
|
|
41
|
+
context.runtime.executePendingJobs()
|
|
42
|
+
|
|
43
|
+
console.log("awaiting")
|
|
44
|
+
const promised = (await promise).unwrap()
|
|
45
|
+
console.log(`${console.depth}:`, { promised, type: context.typeof(promised) })
|
|
46
|
+
|
|
47
|
+
export const result = "hello " + context.dump(context.getProp(promised, "result"))
|
|
48
|
+
|
|
49
|
+
console.log(`${console.depth}: done`)
|
package/package.json
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@componentor/quickjs-for-quickjs",
|
|
3
|
+
"version": "0.31.2",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "https://github.com/componentor/quickjs-emscripten"
|
|
8
|
+
},
|
|
9
|
+
"author": {
|
|
10
|
+
"name": "Componentor"
|
|
11
|
+
},
|
|
12
|
+
"files": [
|
|
13
|
+
"LICENSE",
|
|
14
|
+
"yodawg.jpg",
|
|
15
|
+
"example/**/*",
|
|
16
|
+
"dist/**/*"
|
|
17
|
+
],
|
|
18
|
+
"types": "dist/index.d.mts",
|
|
19
|
+
"main": "dist/index.mjs",
|
|
20
|
+
"module": "dist/index.mjs",
|
|
21
|
+
"exports": {
|
|
22
|
+
"./package.json": "./package.json",
|
|
23
|
+
".": {
|
|
24
|
+
"types": "./dist/index.d.mts",
|
|
25
|
+
"import": "./dist/index.mjs",
|
|
26
|
+
"default": "./dist/index.mjs"
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
"devDependencies": {
|
|
30
|
+
"@componentor/quickjs-asmjs-mjs-release-sync": "0.31.2",
|
|
31
|
+
"@jitl/tsconfig": "0.31.2",
|
|
32
|
+
"@componentor/quickjs-emscripten-core": "0.31.2"
|
|
33
|
+
},
|
|
34
|
+
"scripts": {
|
|
35
|
+
"check": "npx tsc --project . --noEmit",
|
|
36
|
+
"build": "npx tsup",
|
|
37
|
+
"clean": "git clean -fx dist"
|
|
38
|
+
}
|
|
39
|
+
}
|
package/yodawg.jpg
ADDED
|
Binary file
|