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

Eval scope limitation in Salesforce LWC impact the use of the lib #394

Open
ethpsantos opened this issue Nov 14, 2024 · 0 comments
Open

Comments

@ethpsantos
Copy link

ethpsantos commented Nov 14, 2024

Hello.

I have more a point referent at using your (amazing) lib than exactly a bug.
In Salesforce Lighting Web Component the eval function have fixed scope, that means the "this" will always be the global window and you can't change that.
That makes the 'noSandbox = false' to not work because the iframe create won't even have an eval function.
And in 'noSandbox = true' you will face the issue with the scope.

I found a solution for what I'm using that either involve INS and HTML.
For INS I used a function passing the scope.
While for HTML I got the content direct (that has the ` comenda for variable replacement) and run inside the function doing the replacements.

	      let wrapper;
	      if (ctx.cmdName == "HTML") {
	        wrapper = new Function('return ' + context[context.__code__] + ';');
	      } else {
	        wrapper = new Function("return " + context.__code__ + ";");
	      }
	      result = await wrapper.call(context);
  • In processTemplate.ts I had to set the cmdName to ctx (ctx.cmdName = cmdName) before jsSandbox.ts call.

It is working very well for me but maybe are limitations or some issues I'm not having here yet.
I wanted to have opinions and feedback from the guys that developed the lib or have more experience with it. If you believe I will have any big issue in the future. Or any other alternative that the community faced with similar situation with eval.

PS. Another alternative was to put all the data inside the window global so the Eval works like intended... it worked but the window global on Salesforce has so many variables that become trash elements to the API that can end generating issues... even more because my tokens have all the Fields names of objects in the Salesforce to be easy to get it via QUERY... so if a field has the same name than a element in global window it will generate issues... I avoided taking that path.

Thanks for the amazing work in the lib.
Sorry for my bad English.

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

No branches or pull requests

1 participant