Skip to content

Commit

Permalink
refactor: update default scope and this for interpreter
Browse files Browse the repository at this point in the history
  • Loading branch information
jorenrui committed Feb 5, 2024
1 parent 903dd20 commit d5a8da4
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
5 changes: 1 addition & 4 deletions lib/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,7 @@ export default class Entity {
async _interpret(expr, options = {}) {
const Engine = options.isClass ? ClassInterpreter : Interpreter
const engine = new Engine(expr, options)
const ids = {
'$': 'document.querySelector',
'this': 'this.element',
};
const ids = { '$': 'document.querySelector' };

this.variables.forEach((variable) => {
if (variable.startsWith('el.')) {
Expand Down
30 changes: 21 additions & 9 deletions lib/generators/interpreter.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ import { Lexer } from './lexer.js'

const AsyncFunction = Object.getPrototypeOf(async function(){}).constructor

const DEFAULT_SCOPE = {
window: {},
eval: {},
XMLHttpRequest: {},
Function: {},
};

function safeAsyncFunction(context, code, isExpression) {
try {
const expression = !isExpression
Expand Down Expand Up @@ -30,15 +37,20 @@ export class Interpreter extends Lexer {
super(code, options)
}

async interpret(context) {
async interpret(context, ids) {
const code = super.output()
const scope = { proxyWindow: MiniJS.window, this: context }
const scope = { ...DEFAULT_SCOPE, proxyWindow: MiniJS.window }

return await safeAsyncFunction(context, code, this.isExpression)(scope)
try {
return await safeAsyncFunction(context.element, code, this.isExpression)(scope)
} catch (error) {
console.log(code)
console.log(error)
return
}
}
}


export class ClassInterpreter extends Lexer {
constructor(code, options) {
super(code, { ...options, isClass: true })
Expand All @@ -47,18 +59,18 @@ export class ClassInterpreter extends Lexer {

async interpret(context) {
const classNames = super.output()
const scope = { proxyWindow: MiniJS.window, this: context }
const scope = { ...DEFAULT_SCOPE, proxyWindow: MiniJS.window }

let newClassNames = [...this._baseClasses]

if (typeof classNames === 'string') {
const result = await safeAsyncFunction(context, classNames, this.isExpression)(scope)
const result = await safeAsyncFunction(context.element, classNames, this.isExpression)(scope)
newClassNames = newClassNames.concat((result ?? '').split(' '))
} else if (Array.isArray(classNames)) {
for (const conditional of classNames) {
const condition = await safeAsyncFunction(context, conditional.test, true)(scope)
const consequent = await safeAsyncFunction(context, conditional.consequent, conditional.isExpression)(scope)
const alternate = await safeAsyncFunction(context, conditional.alternate, conditional.isExpression)(scope)
const condition = await safeAsyncFunction(context.element, conditional.test, true)(scope)
const consequent = await safeAsyncFunction(context.element, conditional.consequent, conditional.isExpression)(scope)
const alternate = await safeAsyncFunction(context.element, conditional.alternate, conditional.isExpression)(scope)

const consequentClasses = consequent.split(' ')
const alternateClasses = alternate.split(' ')
Expand Down

0 comments on commit d5a8da4

Please sign in to comment.