-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
160 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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:*" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"extends": "@jitl/tsconfig/tsconfig.json", | ||
"include": ["src/**/*"], | ||
"compilerOptions": { | ||
"rootDir": "src" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters