Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Executing scripts in different scopes? #158

Open
JamesB7 opened this issue Aug 13, 2019 · 3 comments
Open

Executing scripts in different scopes? #158

JamesB7 opened this issue Aug 13, 2019 · 3 comments
Labels

Comments

@JamesB7
Copy link

JamesB7 commented Aug 13, 2019

Hello,

I would like, when executing a script, it to use a specific object. I don't want to get rid of the global object, I just want functions that the script defines to go into that specific object. Kind of like the window object on a browser. In this case, I have multiple scripts and multiple objects and I want them to share a global state but still contribute their own functions to particular objects I define.

How would I go about doing this in Jurassic? (Aside from modifying the code being executed itself. Problem there is it'd mess up debug line numbers, etc.)

Thanks,

James

@paulbartrum
Copy link
Owner

I think the window object is just a reference to the global object i.e.

Function('return this')() === window // returns true

Have you tried something like this (untested)?

var scope = ObjectScope.CreateRuntimeScope(null, yourScopeObject, providesImplicitThisValue: false, canDeclareVariables: true);
GlobalObject.Eval(scriptEngine, "some JS code goes here", scope, thisObject, strictMode: true)

yourScopeObject is the object you want to use instead of the global object. thisObject is the value of the this keyword.

@JamesB7
Copy link
Author

JamesB7 commented Aug 22, 2019

I looked on the object I set as yourScopeObject, and I don't see the functions I defined. Is there anything else I ought try? Essentially, if I say

function xyz() { }

I am hoping it will define the function on yourScopeObject. No luck so far.

@JamesB7
Copy link
Author

JamesB7 commented Aug 22, 2019

(Also, FWIW, CreateRuntimeScope's documentation says the first parameter may not be null.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants