Skip to content

Commit

Permalink
feat: jsport编译类型修改为esm并上传至npm,包名dicescript
Browse files Browse the repository at this point in the history
  • Loading branch information
fy0 committed Nov 2, 2023
1 parent 59e0e0e commit 053ea09
Show file tree
Hide file tree
Showing 9 changed files with 325 additions and 89 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/jsport.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ jobs:
run: peg -switch -inline roll.peg

- name: Build
run: gopherjs build github.com/sealdice/dicescript/jsport -o jsport/dicescript.js
run: gopherjs build github.com/sealdice/dicescript/jsport -o jsport/dicescript.cjs

- name: Bundle ESM
run: cd ./jsport && yarn && yarn build

- name: Deploy
uses: peaceiris/actions-gh-pages@v3
Expand Down
7 changes: 7 additions & 0 deletions jsport/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
node_modules
dicescript.cjs
dicescript.cjs.map
index.html
main.go
index.js
webpack.config.js
176 changes: 176 additions & 0 deletions jsport/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
export declare function newVM(): DiceScriptContext;
export declare function newVMForPlaygournd(): DiceScriptContext;
export declare const help: string;

export declare function vmNewDict(): VMValue;
export declare function vmNewFloat(): VMValue;
export declare function vmNewInt(): VMValue;
export declare function vmNewStr(): VMValue;
export declare function newValueMap(): ValueMap;
export declare function newConfig(): RollConfig;

declare interface GoError {
$type: 'errors.*error';
/** error info */
Error(): string;
}

declare interface ValueMap {
$type: 'github.com/sealdice/dicescript.*ValueMap';

Load(key: string): [VMValue | null, boolean];
MustLoad(key: string): VMValue | null;
Store(key: string, value: VMValue): void;
LoadOrStore(key: string, value: VMValue): [VMValue, boolean];
LoadAndDelete(key: string): [VMValue | null, boolean];
Delete(key: string): void;
Range(f: (key: string, value: VMValue) => boolean): void;

__internal_object__: {
$val: any;
mu: any;
read: any;
dirty: boolean;
misses: number;
};
}

declare interface VMValue {
$type: 'github.com/sealdice/dicescript.*VMValue';

ToJSONRaw(save: Map<VMValue, boolean>): [Uint8Array, GoError];
ToJSON(): [Uint8Array, GoError];
UnmarshalJSON(input: Uint8Array): GoError;
ArrayItemGet(ctx: DiceScriptContext, index: number): VMValue | null;
ArrayItemSet(ctx: DiceScriptContext, index: number, val: VMValue): boolean;
ArrayFuncKeepBase(ctx: DiceScriptContext, pickNum: number, orderType: number): [boolean, number];
ArrayFuncKeepHigh(ctx: DiceScriptContext, pickNum: number): [boolean, number];
ArrayFuncKeepLow(ctx: DiceScriptContext, pickNum: number): [boolean, number];
Clone(): VMValue;
AsBool(): boolean;
ToString(): string;
// toStringRaw(ri: recursionInfo): string;
// toReprRaw(ri: recursionInfo): string;
ToRepr(): string;
ReadInt(): [number, boolean];
ReadFloat(): [number, boolean];
ReadString(): [string, boolean];
// ReadArray(): [ArrayData, boolean];
// ReadComputed(): [ComputedData, boolean];
// ReadDictData(): [DictData, boolean];
// MustReadDictData(): DictData;
// MustReadArray(): ArrayData;
MustReadInt(): number;
MustReadFloat(): number;
// ReadFunctionData(): [FunctionData, boolean];
// ReadNativeFunctionData(): [NativeFunctionData, boolean];
// ReadNativeObjectData(): [NativeObjectData, boolean];
OpAdd(ctx: DiceScriptContext, v2: VMValue): VMValue;
OpSub(ctx: DiceScriptContext, v2: VMValue): VMValue;
OpMultiply(ctx: DiceScriptContext, v2: VMValue): VMValue;
OpDivide(ctx: DiceScriptContext, v2: VMValue): VMValue;
OpModulus(ctx: DiceScriptContext, v2: VMValue): VMValue;
OpPower(ctx: DiceScriptContext, v2: VMValue): VMValue;
OpNullCoalescing(ctx: DiceScriptContext, v2: VMValue): VMValue;
OpCompLT(ctx: DiceScriptContext, v2: VMValue): VMValue;
OpCompLE(ctx: DiceScriptContext, v2: VMValue): VMValue;
OpCompEQ(ctx: DiceScriptContext, v2: VMValue): VMValue;
OpCompNE(ctx: DiceScriptContext, v2: VMValue): VMValue;
OpCompGE(ctx: DiceScriptContext, v2: VMValue): VMValue;
OpCompGT(ctx: DiceScriptContext, v2: VMValue): VMValue;
OpBitwiseAnd(ctx: DiceScriptContext, v2: VMValue): VMValue;
OpBitwiseOr(ctx: DiceScriptContext, v2: VMValue): VMValue;
OpPositive(): VMValue;
OpNegation(): VMValue;
AttrSet(ctx: DiceScriptContext, name: string, val: VMValue): VMValue;
AttrGet(ctx: DiceScriptContext, name: string): VMValue;
ItemGet(ctx: DiceScriptContext, index: VMValue): VMValue;
ItemSet(ctx: DiceScriptContext, index: VMValue, val: VMValue): boolean;
GetSlice(ctx: DiceScriptContext, a: number, b: number, step: number): VMValue;
Length(ctx: DiceScriptContext): number;
GetSliceEx(ctx: DiceScriptContext, a: VMValue, b: VMValue): VMValue;
SetSlice(ctx: DiceScriptContext, a: number, b: number, step: number, val: VMValue): boolean;
SetSliceEx(ctx: DiceScriptContext, a: VMValue, b: VMValue, val: VMValue): boolean;
ArrayRepeatTimesEx(ctx: DiceScriptContext, times: VMValue): VMValue;
GetTypeName(): string;
ComputedExecute(ctx: DiceScriptContext): VMValue;
FuncInvoke(ctx: DiceScriptContext, params: VMValue[]): VMValue;
FuncInvokeNative(ctx: DiceScriptContext, params: VMValue[]): VMValue;
AsDictKey(): [string, GoError];

__internal_object__: {
$val: any,
TypeId: any,
Value: any
};
}

declare interface RollConfig {
EnableDiceWoD: boolean;
EnableDiceCoC: boolean;
EnableDiceFate: boolean;
EnableDiceDoubleCross: boolean;

DisableBitwiseOp: boolean;
DisableStmts: boolean;
DisableNDice: boolean;

CallbackLoadVar: (name: string) => [string, VMValue];
CallbackSt: (type: string, name: string, val: VMValue, extra: VMValue, op: string, detail: string) => void;

OpCountLimit: number;
DefaultDiceSideExpr: string;
defaultDiceSideExprCacheFunc: VMValue;

PrintBytecode: boolean;
IgnoreDiv0: boolean;

DiceMinMode: boolean;
DiceMaxMode: boolean;
}


export declare interface DiceScriptContext {
$type: 'github.com/sealdice/dicescript.*Context';

RunExpr(value: string): VMValue | undefined;
/** Eval a code, store result in ctx.Ret, store error in ctx.Error */
Run(expr: string): void;
GetAsmText(): string;
StackTop(): number;
Depth(): number;
Init(): void;
SetConfig(cfg: RollConfig);
loadInnerVar(name: string): VMValue | undefined;
LoadNameGlobal(name: string, isRaw: boolean): VMValue | undefined;
LoadNameLocal(name: string, isRaw: boolean): VMValue | undefined;
LoadName(name: string, isRaw: boolean): VMValue | undefined;
StoreName(name: string, v: VMValue): void;
StoreNameLocal(name: string, v: VMValue): void;
StoreNameGlobal(name: string, v: VMValue): void;

stack: VMValue[];
top: number;
NumOpCount: number;
Error: GoError;
Ret: VMValue | null;
RestInput: string;
Matched: string;
Detail: string;
IsRunning: boolean;
readonly Config: RollConfig;
// flagsStack: RollConfig[];
// CustomDiceInfo: customDiceItem[];
ValueStoreHookFunc: (ctx: DiceScriptContext, name: string, v: VMValue) => boolean;
globalNames: ValueMap;
GlobalValueStoreFunc: (name: string, v: VMValue) => void;
GlobalValueLoadFunc: (name: string) => VMValue;

__internal_object__: {
$val: any;
parser: any;
subThreadDepth: number;
attrs: any;
upCtx: any;
};
}
123 changes: 68 additions & 55 deletions jsport/index.html
Original file line number Diff line number Diff line change
@@ -1,78 +1,91 @@
<!DOCTYPE html>
<html lang="zh-cn">

<head>
<meta charset="UTF-8">
<title>DiceScript 测试页</title>
<meta charset="utf-8" name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0"/>
<meta charset="UTF-8">
<title>DiceScript 测试页</title>
<meta charset="utf-8" name="viewport"
content="width=device-width, initial-scale=1.0, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0" />
</head>

<body>
<script src="./dicescript.js?v=2"></script>
<script src="https://unpkg.zhimg.com/vue@3.2.38/dist/vue.global.prod.js"></script>
<script src="https://unpkg.zhimg.com/eruda@2.4.1"></script>
<script>eruda.init();</script>
<!--<script src="./dicescript.js"></script>-->
<script src="https://unpkg.zhimg.com/vue@3.2.38/dist/vue.global.prod.js"></script>
<script src="https://unpkg.zhimg.com/eruda@2.4.1"></script>
<script>eruda.init();</script>

<div id="app">
<div id="app">
<div>
<div v-for="i in items">{{i}}</div>
<div v-for="i in items">{{i}}</div>
</div>
<div>
<div>
<textarea rows="5" id="prompt" v-model="message"></textarea>
</div>
<button @click="doSend">发送</button>
<div>
<textarea rows="5" id="prompt" v-model="message"></textarea>
</div>
<button @click="doSend">发送</button>
</div>

<div style="margin-top: 2rem;">
<div>提示: 还在开发中,支持语法详情看<a target="_blank" href="https://github.com/sealdice/dicescript/blob/main/GUIDE.md">语法指南</a></div>
<div style="margin-left: 2rem">控制台里有上一条指令的字节码。建议多ctrl+f5以免遇到旧版</div>
<div style="margin-left: 2rem">有一个人物卡变量叫player,通过"_测试30"这种形式可以读取出数字30,可用于ra判定</div>
<div>提示: 还在开发中,支持语法详情看<a target="_blank" href="https://github.com/sealdice/dicescript/blob/main/GUIDE.md">语法指南</a>
</div>
<div style="margin-left: 2rem">控制台里有上一条指令的字节码。建议多ctrl+f5以免遇到旧版</div>
<div style="margin-left: 2rem">有一个人物卡变量叫player,通过"_测试30"这种形式可以读取出数字30,可用于ra判定</div>
</div>
</div>
</div>

<script>
<script type="module">
import ds from "./dist/main.mjs";
const { createApp } = Vue
let ctx = dice.newVM();
ctx.Config.PrintBytecode = true; // 好像没什么用
ctx.Config.EnableDiceWoD = true;
ctx.Config.EnableDiceCoC = true;
ctx.Config.EnableDiceFate = true;
ctx.Config.EnableDiceDoubleCross = true;

let ctx = ds.newVMForPlaygournd();
console.log('module', ds);
console.log('vm', ctx);

const c = ds.newConfig();
c.OpCountLimit = 30000
c.PrintBytecode = true;
c.EnableDiceWoD = true;
c.EnableDiceCoC = true;
c.EnableDiceFate = true;
c.EnableDiceDoubleCross = true;
ctx.SetConfig(c);

createApp({
data() {
return {
items: ['DiceScript Shell'],
message: 'd20'
}
},
methods: {
doSend () {
this.message = this.message.trim()
if (this.message === '') return;
data() {
return {
items: ['DiceScript Shell'],
message: 'd20'
}
},
methods: {
doSend() {
this.message = this.message.trim()
if (this.message === '') return;

this.items.push(`>>> ${this.message}`)
try {
ctx.Run(this.message)
if (ctx.Error) {
this.items.push(`错误: ${ctx.Error.Error()}`)
} else {
this.items.push("过程:" + ctx.Detail)
this.items.push("结果:" + ctx.Ret.ToString())
this.items.push(`栈顶: ${ctx.StackTop()} 层数:${ctx.Depth()} 算力: ${ctx.NumOpCount}`)
this.items.push(`>>> ${this.message}`)
try {
ctx.Run(this.message)
if (ctx.Error) {
this.items.push(`错误: ${ctx.Error.Error()}`)
} else {
this.items.push("过程:" + ctx.Detail)
this.items.push("结果:" + ctx.Ret.ToString())
this.items.push(`栈顶: ${ctx.StackTop()} 层数:${ctx.Depth()} 算力: ${ctx.NumOpCount}`)

if (ctx.RestInput) {
this.items.push(`剩余文本: ${ctx.RestInput}`)
}
console.log(`剩余文本: ${ctx.RestInput || '无'}`)
}
} catch (e) {
this.items.push(`内部错误: ` + e.message)
}
console.log(ctx.GetAsmText())
this.message = '';
if (ctx.RestInput) {
this.items.push(`剩余文本: ${ctx.RestInput}`)
}
console.log(`剩余文本: ${ctx.RestInput || '无'}`)
}
} catch (e) {
this.items.push(`内部错误: ` + e.message)
}
console.log(ctx.GetAsmText())
this.message = '';
}
}
}).mount('#app')
</script>
</script>
</body>
</html>

</html>
3 changes: 3 additions & 0 deletions jsport/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { ds } from './dicescript.cjs';

export default ds;
Loading

0 comments on commit 053ea09

Please sign in to comment.