JS-Interpreter icon indicating copy to clipboard operation
JS-Interpreter copied to clipboard

Binding the interpreter to an external envirotnment

Open photopea opened this issue 6 years ago • 0 comments

I want the interpreted code to access data, which are not in the memory of the interpreter.

There should be a global object "app", similar to "window", which has many properties and methods. Some properties of the "app" have their own properties and methods. Maby my case is similar e.g. to giving scripts the access to HTML5 Canvas drawing.

Whenever a sub-property of "app" is read, or changed, or a method is called, I need my own functions to be called, which will manipulate my data (in my own representation) and possibly return a value.

My idea is, that I could give the interpreter three functions:

getProp(Obj, propName), 
setProp(Obj, propName, newValue)
callFnc(Obj, funcName, params)

When the interpreted script tries to read the property, which does not exist, getProp(Obj, propName) would be called, which would return the necessary value. Same for setting a property and calling a method.

The first accessing of the "app" would return a special object, and all other accesses to that object would perform some actions outside the environment of the interpreter.

Is there any way to do this in this interpreter?

photopea avatar Sep 06 '19 22:09 photopea