Resolve #5755
This commit is contained in:
@ -2,6 +2,8 @@ import autobind from 'autobind-decorator';
|
||||
import * as seedrandom from 'seedrandom';
|
||||
import { Variable, PageVar, envVarsDef, funcDefs, Block, isFnBlock } from '.';
|
||||
import { version } from '../../config';
|
||||
import { AiScript, utils, parse, values } from '@syuilo/aiscript';
|
||||
import { createAiScriptEnv } from '../create-aiscript-env';
|
||||
|
||||
type Fn = {
|
||||
slots: string[];
|
||||
@ -15,15 +17,41 @@ export class ASEvaluator {
|
||||
private variables: Variable[];
|
||||
private pageVars: PageVar[];
|
||||
private envVars: Record<keyof typeof envVarsDef, any>;
|
||||
public aiscript: AiScript;
|
||||
private pageVarUpdatedCallback;
|
||||
|
||||
private opts: {
|
||||
randomSeed: string; visitor?: any; page?: any; url?: string;
|
||||
};
|
||||
|
||||
constructor(variables: Variable[], pageVars: PageVar[], opts: ASEvaluator['opts']) {
|
||||
constructor(vm: any, variables: Variable[], pageVars: PageVar[], opts: ASEvaluator['opts']) {
|
||||
this.variables = variables;
|
||||
this.pageVars = pageVars;
|
||||
this.opts = opts;
|
||||
this.aiscript = new AiScript({ ...createAiScriptEnv(vm, {
|
||||
storageKey: 'pages:' + opts.page.id
|
||||
}), ...{
|
||||
'MkPages:updated': values.FN_NATIVE(([callback]) => {
|
||||
this.pageVarUpdatedCallback = callback;
|
||||
})
|
||||
}}, {
|
||||
in: (q) => {
|
||||
return new Promise(ok => {
|
||||
vm.$root.dialog({
|
||||
title: q,
|
||||
input: {}
|
||||
}).then(({ canceled, result: a }) => {
|
||||
ok(a);
|
||||
});
|
||||
});
|
||||
},
|
||||
out: (value) => {
|
||||
console.log(value);
|
||||
},
|
||||
log: (type, params) => {
|
||||
},
|
||||
maxStep: 16384
|
||||
});
|
||||
|
||||
const date = new Date();
|
||||
|
||||
@ -50,6 +78,9 @@ export class ASEvaluator {
|
||||
const pageVar = this.pageVars.find(v => v.name === name);
|
||||
if (pageVar !== undefined) {
|
||||
pageVar.value = value;
|
||||
if (this.pageVarUpdatedCallback) {
|
||||
this.aiscript.execFn(this.pageVarUpdatedCallback, [values.STR(name), utils.jsToVal(value)]);
|
||||
}
|
||||
} else {
|
||||
throw new AoiScriptError(`No such page var '${name}'`);
|
||||
}
|
||||
@ -110,6 +141,10 @@ export class ASEvaluator {
|
||||
return scope.getState(block.value);
|
||||
}
|
||||
|
||||
if (block.type === 'aiScriptVar') {
|
||||
return utils.valToJs(this.aiscript.scope.get(block.value));
|
||||
}
|
||||
|
||||
if (isFnBlock(block)) { // ユーザー関数定義
|
||||
return {
|
||||
slots: block.value.slots.map(x => x.name),
|
||||
|
@ -95,6 +95,7 @@ export const literalDefs: Record<string, { out: any; category: string; icon: any
|
||||
textList: { out: 'stringArray', category: 'value', icon: faList, },
|
||||
number: { out: 'number', category: 'value', icon: faSortNumericUp, },
|
||||
ref: { out: null, category: 'value', icon: faMagic, },
|
||||
aiScriptVar: { out: null, category: 'value', icon: faMagic, },
|
||||
fn: { out: 'function', category: 'value', icon: faSquareRootAlt, },
|
||||
};
|
||||
|
||||
|
@ -1,6 +1,7 @@
|
||||
import { utils, values } from '@syuilo/aiscript';
|
||||
|
||||
export function createAiScriptEnv(vm, opts) {
|
||||
let apiRequests = 0;
|
||||
return {
|
||||
USER_ID: values.STR(vm.$store.state.i.id),
|
||||
USER_USERNAME: values.STR(vm.$store.state.i.username),
|
||||
@ -21,6 +22,8 @@ export function createAiScriptEnv(vm, opts) {
|
||||
return confirm.canceled ? values.FALSE : values.TRUE
|
||||
}),
|
||||
'Mk:api': values.FN_NATIVE(async ([ep, param, token]) => {
|
||||
apiRequests++;
|
||||
if (apiRequests > 16) return values.NULL;
|
||||
const res = await vm.$root.api(ep.value, utils.valToJs(param), token || null);
|
||||
return utils.jsToVal(res);
|
||||
}),
|
||||
|
Reference in New Issue
Block a user