Skip to content

Commit 33ca9ce

Browse files
authored
feat: add docs, add array/string/date prototype function (#25)
1 parent 3dbc8ce commit 33ca9ce

33 files changed

+4193
-225
lines changed

.github/workflows/deploy-docs.yaml

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
name: Deploy docs
2+
3+
on:
4+
push:
5+
branches: [main]
6+
7+
workflow_dispatch:
8+
9+
permissions:
10+
contents: read
11+
pages: write
12+
id-token: write
13+
14+
concurrency:
15+
group: pages
16+
cancel-in-progress: false
17+
18+
jobs:
19+
# Build job
20+
build:
21+
runs-on: ubuntu-latest
22+
steps:
23+
- name: Checkout
24+
uses: actions/checkout@v4
25+
with:
26+
fetch-depth: 0 # Not needed if lastUpdated is not enabled
27+
- uses: pnpm/action-setup@v3 # pnpm is optional but recommended, you can also use npm / yarn
28+
with:
29+
version: 8
30+
- name: Setup Node
31+
uses: actions/setup-node@v4
32+
with:
33+
node-version: 18
34+
cache: pnpm
35+
- name: Setup Pages
36+
uses: actions/configure-pages@v5
37+
- name: Install dependencies
38+
run: pnpm install
39+
- name: Build with Rspress
40+
run: |
41+
pnpm run docs:build
42+
- name: Upload artifact
43+
uses: actions/upload-pages-artifact@v3
44+
with:
45+
path: doc_build
46+
47+
# Deployment job
48+
deploy:
49+
environment:
50+
name: github-pages
51+
url: ${{ steps.deployment.outputs.page_url }}
52+
needs: build
53+
runs-on: ubuntu-latest
54+
name: Deploy
55+
steps:
56+
- name: Deploy to GitHub Pages
57+
id: deployment
58+
uses: actions/deploy-pages@v4

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,6 @@ dist
123123
*.patch
124124
result.json
125125
.result/
126+
127+
# docs
128+
doc_build/

benchmark/cases/v8-7/_build.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const files = [
66
'deltablue',
77
// 'earley-boyer',
88
'navier-stokes',
9-
// 'raytrace',
9+
'raytrace',
1010
// 'regexp',
1111
'richards',
1212
'splay',

docs/index.md

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
pageType: home
3+
4+
hero:
5+
name: jsscript
6+
tagline: A JavaScript VM written in TypeScript.
7+
actions:
8+
- theme: brand
9+
text: Try
10+
link: /repl/
11+
- theme: alt
12+
text: Github
13+
link: https://github.com/XGHeaven/jsscript
14+
features:
15+
- title: Flexible
16+
details: You can freely assemble various functionalities, from a full-featured virtual machine to a simple expression evaluator, and easily implement them.
17+
# icon: 🏃🏻‍♀️
18+
- title: Support ESNext
19+
details: JavaScript virtual machine support ESNext as the main target
20+
# icon: 📦
21+
- title: Maintainability
22+
details: This project is implemented in TypeScript. Type Safe. Easy Learning.
23+
# icon: 🎨
24+
---

docs/repl.tsx

+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
import { useRef, useEffect, useImperativeHandle, Ref } from 'react'
2+
import ReactAce from 'react-ace'
3+
import 'ace-builds/src-noconflict/mode-javascript'
4+
import 'ace-builds/src-noconflict/theme-monokai'
5+
import 'ace-builds/src-noconflict/ext-language_tools'
6+
import { Runtime, Features, parseScript, toHostValue } from '../src'
7+
8+
import 'luna-object-viewer/luna-object-viewer.css'
9+
import 'luna-data-grid/luna-data-grid.css'
10+
import 'luna-dom-viewer/luna-dom-viewer.css'
11+
import 'luna-console/luna-console.css'
12+
import LunaConsole from 'luna-console'
13+
14+
import { Button } from 'rspress/theme'
15+
16+
console.log(LunaConsole)
17+
18+
export const frontmatter = {
19+
pageType: 'custom',
20+
}
21+
22+
const ConsolePanel = (props: { handlerRef: Ref<Partial<Console>> }) => {
23+
const domRef = useRef<HTMLDivElement | null>(null)
24+
const instRef = useRef<LunaConsole | null>(null)
25+
useEffect(() => {
26+
instRef.current = new LunaConsole(domRef.current!)
27+
}, [])
28+
useImperativeHandle(
29+
props.handlerRef,
30+
() => ({
31+
log: (...args: any[]) => {
32+
instRef.current?.log(...args)
33+
},
34+
info: (...args: any[]) => {
35+
instRef.current?.info(...args)
36+
},
37+
error: (...args: any[]) => {
38+
instRef.current?.error(...args)
39+
},
40+
clear: () => {
41+
instRef.current?.clear()
42+
},
43+
debug: (...args: any[]) => {
44+
instRef.current?.debug(...args)
45+
},
46+
}),
47+
[]
48+
)
49+
return <div style={{ width: '100%', height: '100%', overflow: 'auto' }} ref={domRef}></div>
50+
}
51+
52+
const defaultScript = `
53+
console.log('Hello World')
54+
`.trim()
55+
56+
export default () => {
57+
const content = useRef(defaultScript)
58+
const handlerRef = useRef<Partial<Console>>({})
59+
60+
const run = () => {
61+
handlerRef.current.clear?.()
62+
try {
63+
const runtime = new Runtime({
64+
features: [
65+
new Features.ECMA262Feature(),
66+
new Features.OsFeature(),
67+
new Features.ConsoleObjectFeature({
68+
consoleObject: handlerRef.current,
69+
}),
70+
],
71+
})
72+
const context = runtime.newContext()
73+
const parseBefore = Date.now()
74+
const fn = parseScript(context, content.current)
75+
const runBefore = Date.now()
76+
context.run(fn)
77+
if (runtime.currentException) {
78+
handlerRef.current.error?.(toHostValue(runtime.currentException))
79+
}
80+
handlerRef.current.debug?.(
81+
`Finished. Compile time: ${runBefore - parseBefore}ms, Execution time: ${Date.now() - runBefore}ms`
82+
)
83+
} catch (e) {
84+
handlerRef.current.error?.(e)
85+
}
86+
}
87+
88+
return (
89+
<div style={{ display: 'flex', flexDirection: 'row', height: 'calc(100vh - 72px)' }}>
90+
<ReactAce
91+
defaultValue={defaultScript}
92+
style={{ width: '100%', height: '100%' }}
93+
mode="javascript"
94+
theme="monokai"
95+
onChange={(newValue) => (content.current = newValue)}
96+
/>
97+
<div style={{ width: '100%', height: '100%', display: 'flex', flexDirection: 'column' }}>
98+
<div style={{ padding: '8px 16px', flex: 'none' }}>
99+
<span onClick={run}>
100+
<Button text="Run" type="button"></Button>
101+
</span>
102+
</div>
103+
<ConsolePanel handlerRef={handlerRef} />
104+
</div>
105+
</div>
106+
)
107+
}

package.json

+18-5
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,38 @@
66
"main": "./dist/index.js",
77
"typings": "./dist/index.d.ts",
88
"scripts": {
9-
"play": "tsc; node ./dist/playground.js",
9+
"play": "tsc -p tsconfig.build.json; node ./dist/playground.js",
1010
"test262": "test262-harness --hostType engine262 --hostPath ./bin/jsscript --host-args=\"run --test262\" --saveOnlyFailed",
1111
"test262:ci": "node ./scripts/test262.js",
1212
"test262:analysis": "node ./scripts/analysis.js",
13-
"build": "tsc",
14-
"fmt": "prettier -w './{src,scripts,benchmark}/**/*.{ts,js}'",
15-
"fmt:check": "prettier -c './{src,scripts,benchmark}/**/*.{ts,js}'",
16-
"bench": "bash ./benchmark/run.sh"
13+
"build": "tsc -p tsconfig.build.json",
14+
"fmt": "prettier -w './{src,scripts,benchmark,docs}/**/*.{ts,tsx,js,md}'",
15+
"fmt:check": "prettier -c './{src,scripts,benchmark,docs}/**/*.{ts,tsx,js,md}'",
16+
"bench": "bash ./benchmark/run.sh",
17+
"docs:dev": "rspress dev",
18+
"docs:build": "rspress build"
1719
},
1820
"files": [
1921
"dist"
2022
],
2123
"author": "XGHeaven <[email protected]>",
2224
"license": "MIT",
2325
"devDependencies": {
26+
"@rspack/core": "^0.7.3",
2427
"@types/node": "^20.10.4",
28+
"@types/react": "^18.3.3",
2529
"@types/yargs": "^17.0.32",
30+
"ace-builds": "^1.35.0",
2631
"eval5": "^1.4.7",
32+
"luna-console": "^1.3.3",
33+
"luna-data-grid": "^0.6.0",
34+
"luna-dom-viewer": "^1.3.0",
35+
"luna-object-viewer": "^0.3.1",
2736
"prettier": "^3.1.1",
37+
"process": "^0.11.10",
38+
"react": "^18.3.1",
39+
"react-ace": "^11.0.1",
40+
"rspress": "^1.24.0",
2841
"sablejs": "^1.1.0",
2942
"test262-harness": "^9.2.0",
3043
"typescript": "^5.4.3"

0 commit comments

Comments
 (0)