Skip to content

Commit

Permalink
update sm
Browse files Browse the repository at this point in the history
  • Loading branch information
dragazo committed Oct 4, 2024
1 parent 8b89bc3 commit f939987
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 25 deletions.
43 changes: 34 additions & 9 deletions extensions/StateMachine/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,20 @@
new Extension.PaletteCategory(
'StateMachine',
[
new Extension.Palette.Block('smTransition'),
new Extension.Palette.Block('smInState'),
new Extension.Palette.Block('smTransition'),
'-',
new Extension.Palette.Block('smMarkVar'),
],
SpriteMorph
),
new Extension.PaletteCategory(
'StateMachine',
[
new Extension.Palette.Block('smTransition'),
new Extension.Palette.Block('smInState'),
new Extension.Palette.Block('smTransition'),
'-',
new Extension.Palette.Block('smMarkVar'),
],
StageMorph
),
Expand All @@ -57,6 +61,14 @@

getBlocks() {
return [
new Extension.Block(
'smInState',
'predicate',
'StateMachine',
'%var in state %s ?',
[],
function (v0, v1) { return window.StateMachine_fns.in_state(this, v0, v1); }
).for(SpriteMorph, StageMorph),
new Extension.Block(
'smTransition',
'command',
Expand All @@ -66,19 +78,31 @@
function (v0, v1) { return window.StateMachine_fns.transition(this, v0, v1); }
).terminal().for(SpriteMorph, StageMorph),
new Extension.Block(
'smInState',
'predicate',
'smMarkVar',
'command',
'StateMachine',
'%var in state %s ?',
[],
function (v0, v1) { return window.StateMachine_fns.check_state(this, v0, v1); }
'mark var %var as %smVarType',
[null, 'internal'],
function (v0, v1) { return window.StateMachine_fns.mark_var(v0, v1); }
).for(SpriteMorph, StageMorph),

];
}

getLabelParts() {
return [
new Extension.LabelPart(
'smVarType',
() => {
const part = new InputSlotMorph(
null, // text
false, // numeric
{"internal": "internal","input": "input","output": "output",}, // options
true // readonly
);
return part;
}
),

];
}
Expand All @@ -90,14 +114,15 @@
path = path.substring(0, path.lastIndexOf("/"));
var s = document.createElement('script');
s.type = "module";
s.innerHTML = `import init, {check_state, copy_stateflow_code, transition, visualize} from '${path}/pkg/netsblox_stateflow_ext.js';
s.innerHTML = `import init, {copy_stateflow_code, in_state, mark_var, transition, visualize} from '${path}/pkg/netsblox_stateflow_ext.js';
await init();
window.StateMachine_fns = {};
window.StateMachine_fns.check_state = check_state;
window.StateMachine_fns.copy_stateflow_code = copy_stateflow_code;
window.StateMachine_fns.in_state = in_state;
window.StateMachine_fns.mark_var = mark_var;
window.StateMachine_fns.transition = transition;
window.StateMachine_fns.visualize = visualize;
`;
Expand Down
14 changes: 10 additions & 4 deletions extensions/StateMachine/pkg/netsblox_stateflow_ext.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,20 @@ export function copy_stateflow_code(): void;
* @param {any} proc
* @param {any} machine
* @param {any} state
* @returns {boolean}
*/
export function transition(proc: any, machine: any, state: any): void;
export function in_state(proc: any, machine: any, state: any): boolean;
/**
* @param {any} proc
* @param {any} machine
* @param {any} state
* @returns {boolean}
*/
export function check_state(proc: any, machine: any, state: any): boolean;
export function transition(proc: any, machine: any, state: any): void;
/**
* @param {any} _var
* @param {any} _type
*/
export function mark_var(_var: any, _type: any): void;

export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module;

Expand All @@ -30,8 +35,9 @@ export interface InitOutput {
readonly setup: () => void;
readonly visualize: () => void;
readonly copy_stateflow_code: () => void;
readonly in_state: (a: number, b: number, c: number, d: number) => void;
readonly transition: (a: number, b: number, c: number, d: number) => void;
readonly check_state: (a: number, b: number, c: number, d: number) => void;
readonly mark_var: (a: number, b: number) => void;
readonly __wbindgen_malloc: (a: number, b: number) => number;
readonly __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;
readonly __wbindgen_add_to_stack_pointer: (a: number) => number;
Expand Down
30 changes: 19 additions & 11 deletions extensions/StateMachine/pkg/netsblox_stateflow_ext.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,16 +202,19 @@ export function copy_stateflow_code() {
* @param {any} proc
* @param {any} machine
* @param {any} state
* @returns {boolean}
*/
export function transition(proc, machine, state) {
export function in_state(proc, machine, state) {
try {
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
wasm.transition(retptr, addHeapObject(proc), addHeapObject(machine), addHeapObject(state));
wasm.in_state(retptr, addHeapObject(proc), addHeapObject(machine), addHeapObject(state));
var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
if (r1) {
throw takeObject(r0);
var r2 = getDataViewMemory0().getInt32(retptr + 4 * 2, true);
if (r2) {
throw takeObject(r1);
}
return r0 !== 0;
} finally {
wasm.__wbindgen_add_to_stack_pointer(16);
}
Expand All @@ -221,24 +224,29 @@ export function transition(proc, machine, state) {
* @param {any} proc
* @param {any} machine
* @param {any} state
* @returns {boolean}
*/
export function check_state(proc, machine, state) {
export function transition(proc, machine, state) {
try {
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
wasm.check_state(retptr, addHeapObject(proc), addHeapObject(machine), addHeapObject(state));
wasm.transition(retptr, addHeapObject(proc), addHeapObject(machine), addHeapObject(state));
var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
var r2 = getDataViewMemory0().getInt32(retptr + 4 * 2, true);
if (r2) {
throw takeObject(r1);
if (r1) {
throw takeObject(r0);
}
return r0 !== 0;
} finally {
wasm.__wbindgen_add_to_stack_pointer(16);
}
}

/**
* @param {any} _var
* @param {any} _type
*/
export function mark_var(_var, _type) {
wasm.mark_var(addHeapObject(_var), addHeapObject(_type));
}

function handleError(f, args) {
try {
return f.apply(this, args);
Expand Down
Binary file modified extensions/StateMachine/pkg/netsblox_stateflow_ext_bg.wasm
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ export const memory: WebAssembly.Memory;
export function setup(): void;
export function visualize(): void;
export function copy_stateflow_code(): void;
export function in_state(a: number, b: number, c: number, d: number): void;
export function transition(a: number, b: number, c: number, d: number): void;
export function check_state(a: number, b: number, c: number, d: number): void;
export function mark_var(a: number, b: number): void;
export function __wbindgen_malloc(a: number, b: number): number;
export function __wbindgen_realloc(a: number, b: number, c: number, d: number): number;
export function __wbindgen_add_to_stack_pointer(a: number): number;
Expand Down

0 comments on commit f939987

Please sign in to comment.