Skip to content

Commit

Permalink
types
Browse files Browse the repository at this point in the history
  • Loading branch information
dlopezalvas committed Jul 24, 2024
1 parent 6ef0959 commit 4dbadad
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 14 deletions.
13 changes: 13 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@
},
"devDependencies": {
"@types/jest": "^29.5.12",
"@types/js-beautify": "^1.14.3",
"canvas": "^2.11.2",
"electron": "^22.3.25",
"electron-packager": "^17.1.2",
Expand Down
5 changes: 3 additions & 2 deletions src/components/challengeView/SceneButtons/Execute.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Button } from "@mui/material"
import { scene } from "../scene"
import { interpreterFactory } from "./interpreter-factory"
import Interpreter from "js-interpreter"

export const ExecuteButton = () => {

Expand All @@ -10,11 +11,11 @@ export const ExecuteButton = () => {
executeUntilEnd(interpreter)
}

const executeUntilEnd = (interpreter: any) => {
const executeUntilEnd = (interpreter: Interpreter) => {
return new Promise((success, reject) => {
let moreToExecute: boolean

const executeInterpreter = (interpreter: any) => {
const executeInterpreter = (interpreter: Interpreter) => {
try {
moreToExecute = interpreter.run();
} catch (e) {
Expand Down
20 changes: 12 additions & 8 deletions src/components/challengeView/SceneButtons/interpreter-factory.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import { workspaceToCode } from "../../blockly/blockly"
//@ts-ignore
import Interpreter from 'js-interpreter'
//@ts-ignore
import beautify from 'js-beautify'
import { scene } from "../scene"

export type Behaviour = {
receptor: any
argumentos: any
iniciar(receptor: any): void
actualizar(): void
eliminar(): void
}

type Interpreter = {
setProperty: (scope: any, name: string, ) => void

export type Actor = {
hacer_luego: (behaviour: Behaviour, params: {}) => void
}

class InterpreterFactory {
Expand All @@ -20,7 +24,7 @@ class InterpreterFactory {
* so the only features you will be able to access are detailed
* at the init function, which appears below.
*/
createInterpreter(): Interpreter { ///TODO tipo
createInterpreter(): Interpreter {
return new Interpreter(this.wrappedCode(), (interpreter: any, scope: any) => {
return this.init(interpreter, scope);
})
Expand All @@ -32,7 +36,7 @@ class InterpreterFactory {
* @param interpreter
* @param scope
*/
init(interpreter: any, scope: any): any {
init(interpreter: Interpreter, scope: any): void {
interpreter.setProperty(scope, 'out_hacer', interpreter.createAsyncFunction(this.doWrapper))
interpreter.setProperty(scope, 'highlightBlock', interpreter.createNativeFunction(this.highlightBlock))
interpreter.setProperty(scope, 'evaluar', interpreter.createNativeFunction(this.evaluateWrapper))
Expand Down Expand Up @@ -63,7 +67,7 @@ class InterpreterFactory {
* @param params
* @param callback
*/
doWrapper(behaviour: any, params: any, callback: any) {
doWrapper(behaviour: Behaviour, params: any, callback: () => void) {
const actor = scene.sceneActor()
params = JSON.parse(params ? params.toString() : '')
var behaviourClass = scene.behaviourClass(behaviour ? behaviour.toString() : '')
Expand Down
9 changes: 5 additions & 4 deletions src/components/challengeView/scene.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { adaptURL } from "../../scriptLoader";
import { Challenge } from "../../staticData/challenges";
import { Actor, Behaviour } from "./SceneButtons/interpreter-factory";

class Scene {
iframe(): HTMLIFrameElement {
Expand Down Expand Up @@ -82,15 +83,15 @@ class Scene {
await this.setChallenge(descriptor)
}

sceneActor(): any { //TODO type
sceneActor(): Actor {
return this.eval('pilas.escena_actual().automata')
}

sceneReceptor(receptor: string): any {
sceneReceptor(receptor: string): Actor {
return this.eval(`pilas.escena_actual().${receptor}`)
}

behaviourClass(behaviour: string): any {
behaviourClass(behaviour: string): Behaviour {
return this.eval(`
var comportamiento = null;
Expand All @@ -108,7 +109,7 @@ class Scene {
`)
}

evaluateExpression(expression: string) {
evaluateExpression(expression: string): boolean {
return this.eval(`
try {
var value = pilas.escena_actual().automata.${expression}
Expand Down
12 changes: 12 additions & 0 deletions src/types/js-interpreter.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
declare module 'js-interpreter' {
export default class Interpreter {
createNativeFunction: (func: any) => void
createAsyncFunction: (func: any) => void
constructor(code: string, initFunc?: (interpreter: Interpreter, globalObject: any) => void)
step(): boolean
run(): boolean
appendCode(code: string): void
paused: boolean
setProperty(obj: any, name: string, value: any, descriptor?: PropertyDescriptor): void;
}
}

0 comments on commit 4dbadad

Please sign in to comment.