@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
package/LICENSE
ADDED
|
@@ -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.
|
package/README.md
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
# quickjs-for-quickjs
|
|
2
|
+
|
|
3
|
+

|
|
4
|
+
|
|
5
|
+
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.
|
|
6
|
+
|
|
7
|
+
`quickjs-for-quickjs` has been tested in the following configurations:
|
|
8
|
+
|
|
9
|
+
- ✅ NodeJS -> quickjs-for-quickjs
|
|
10
|
+
- ✅ NodeJS -> quickjs-emscripten RELEASE_SYNC -> quickjs-for-quickjs
|
|
11
|
+
- ❌ NodeJS -> quickjs-emscripten RELEASE_SYNC -> quickjs-for-quickjs -> quickjs-for-quickjs (This hits stack overflow in QuickJS. It could work with a larger stack size for the outer QuickJS instance)
|
|
12
|
+
|
|
13
|
+
## Why?
|
|
14
|
+
|
|
15
|
+
For fun!
|
|
16
|
+
|
|
17
|
+
This could have practical applications, like if you're a user writing a plugin for a 3rd-party system using quickjs-emscripten for scripting, and you as a plugin author also want your plugin to have its own plugin ecosystem.
|
|
18
|
+
|
|
19
|
+
## Usage
|
|
20
|
+
|
|
21
|
+
For a more detailed example, see [the "example" directory][example] in the repo.
|
|
22
|
+
|
|
23
|
+
[example]: https://github.com/justjake/quickjs-emscripte/tree/main/packages/quickjs-for-quickjs/example
|
|
24
|
+
|
|
25
|
+
```javascript
|
|
26
|
+
// nodejs-host.mjs
|
|
27
|
+
import fs from "node:fs/promises"
|
|
28
|
+
import module from "node:module"
|
|
29
|
+
import { getQuickJS, setUpContext } from "quickjs-for-quickjs"
|
|
30
|
+
|
|
31
|
+
// get quickjs source code
|
|
32
|
+
const require = module.createRequire(import.meta.url)
|
|
33
|
+
const quickjsSource = await fs.readFile(require.resolve("quickjs-for-quickjs"), "utf8")
|
|
34
|
+
|
|
35
|
+
// create the host QuickJS context
|
|
36
|
+
const QuickJS = await getQuickJS()
|
|
37
|
+
const context = QuickJS.createContext()
|
|
38
|
+
|
|
39
|
+
// inject console.log, which is required for quickjs-for-quickjs to work
|
|
40
|
+
setUpContext(context)
|
|
41
|
+
|
|
42
|
+
// allow the host QuickJS to import quickjs-for-quickjs module
|
|
43
|
+
context.runtime.setModuleLoader((name) => {
|
|
44
|
+
if (name === "quickjs-for-quickjs") {
|
|
45
|
+
return quickjsSource
|
|
46
|
+
}
|
|
47
|
+
throw new Error(`Module not found: ${name}`)
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
const quickjsHost = `
|
|
51
|
+
// import and create inner QuickJS guest context
|
|
52
|
+
import { getQuickJS } from "quickjs-for-quickjs"
|
|
53
|
+
const context = await getQuickJS().then(mod => mod.createContext())
|
|
54
|
+
|
|
55
|
+
// eval some code in the inner context
|
|
56
|
+
const innerResult = context.evalCode('1 + 2', "quickjs-guest.mjs").unwrap()
|
|
57
|
+
|
|
58
|
+
// export the result
|
|
59
|
+
export const result = innerResult.consume(context.dump)
|
|
60
|
+
`
|
|
61
|
+
|
|
62
|
+
// eval the quickjs host
|
|
63
|
+
const handle = context.evalCode(quickjsHost, "quickjs-host.mjs").unwrap()
|
|
64
|
+
|
|
65
|
+
// get the result
|
|
66
|
+
context.runtime.executePendingJob()
|
|
67
|
+
const result = context.unwrapResult(context.getPromiseState(handle))
|
|
68
|
+
|
|
69
|
+
console.log(result.consume(context.dump))
|
|
70
|
+
```
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { QuickJSWASMModule, QuickJSContext } from '@componentor/quickjs-emscripten-core';
|
|
2
|
+
export { default as RELEASE_SYNC } from '@componentor/quickjs-asmjs-mjs-release-sync';
|
|
3
|
+
|
|
4
|
+
declare function getQuickJS(): Promise<QuickJSWASMModule>;
|
|
5
|
+
declare function newQuickJSWASMModule(): Promise<QuickJSWASMModule>;
|
|
6
|
+
declare function setUpContext(context: QuickJSContext, options?: {
|
|
7
|
+
log?: (...args: unknown[]) => void;
|
|
8
|
+
}): QuickJSContext;
|
|
9
|
+
|
|
10
|
+
export { getQuickJS, newQuickJSWASMModule, setUpContext };
|