Comments (10)
I forked and made the changes to remove the unnecessary check for return outside of functions, and the call to GetCompletionValue() so that Run() would be
public object Run( string js ) {
return Execute(js).GetCompletionValue();
}
https://github.com/greggwon/jint/commit/215d3895edde32bee97cd66e1024b15ba2e6dca6
from jint.
Thank you for looking into this.
First I would suggest to use an anonymous function like this:
(function () {
// your code goes here
})();
This would fix your argument about having to define a named function.
Then, instead of having users to use a return
statement, it would be simpler to just call GetCompletionValue
without wrapping the code in a function, it already works. So in the end, you method is really just removing the need of GetCompletionValue()
.
In this case, would you please change your code to call it Evaluate
and just wrap GetCompletionValue().ToObject()
?
from jint.
@greggwon I was running into a similar issue. Our users can write scripts and sometimes they use a return
statement and sometimes they don't. I found that you can use the Tolerant
flag for the parser to allow a return statement outside of a function.
var engine = new Engine();
engine.Execute("return 0;", new ParserOptions
{
Tolerant = true
});
I don't know if this is the intended use -- but it seems to work.
from jint.
Oh man, I didn't even know that, awesome feature. We should document it somewhere ;)
from jint.
@sebastienros I guess I'm confused -- I thought you had coded the parser. Or are you joking?
from jint.
I am not joking ;) I work on so many different things that I tend to forget after a few months. But his one might also be explained because I actually ported the parser from Esprima, so I did not actually made the logic behind it. Converted to C# and improved some code paths.
from jint.
After looking into the code, the goal of this option is to not throw on parse errors but instead add these error into an internal collection program.Errors
that you can analyze afterwards. Some of the parser errors can then be treated as warning and still be run. This example you are providing is one of the acceptable syntax errors. Maybe it's about time to provide some little documentation as part of the wiki ...
from jint.
I guess my question is, why would you care where i put a return statement? What
does this tolerant flag do, to improve software correctness? In the end,
imbedding javascript for application scripting is always going to end up with an
expression to be executed and a return value is the best way to know that
whatever expression you executed was successfully completed. So, for me, it
makes little sense for the default to be "not" to allow return in the script.
There are times in our app, when I need to debug the script we are running, and
so I return an intermediate value, in the middle of the script by just using
"return intermediatValue;" with the existing code after that. I don't want to
fight with the runtime environment to get real work done. I need to run
javascript and I need to always have the ability to see a return value from that
execution.
This was possible in the previous 0.X version, so I am not sure I understand how
this change adds value to the overall system? Is there some disastrous use case
where a return in the middle of a non-function block of code is somehow a
terrible mistake?
Our code is typically a script that has prepended a bunch of assignments of
values that our app knows about. The script is just a bunch of if statements
with returned values. So, while its possible to do what you show below, the
real questions is, why does it make sense to do this now? What's the
advantage? How does Tolerant = true make the software system more performant or
more useful or more correct? Either it's right to return a value in a script,
or it's not.
This feels like useless overhead. Deferring "correctness" checks to run time
failures just creates more fragile code. In this case, it causes runtime
"exceptions" that are going to terminate the app (.Net should have checked
exceptions, and since it doesn't, people write fragile code, not knowing when to
catch exceptions because they can be added after the code is written, and then
there is now, no way to know about new failure modes without line by line
inspection), rather than improve the software systems' stability and usability.
Gregg Wonderly
On 9/25/2014 11:42 AM, Brian Beard wrote:
@greggwon https://github.com/greggwon I was running into a similar issue.
Our users can write scripts and sometimes they use a |return| statement and
sometimes they don't. I found that you can use the |Tolerant| flag for the
parser to allow a return statement outside of a function.var engine = new Engine();
engine.Execute("return 0;", new ParserOptions
{
Tolerant = true
});I don't know if this is the intended use -- but it seems to work.
—
Reply to this email directly or view it on GitHub
#75 (comment).
from jint.
@sebastienros Ah, that makes sense -- I know what you mean about forgetting. BTW -- Thanks for writing this great library.
from jint.
@greggwon Jint 2 is a complete rewrite, not a single line of code was used from Jint 1 and all decisions have been thrown away, with one direction: following the specification. And the specification states that a return
statement is invalid outside of a function.
Btu what is great is that you have solutions for that, your only issue is to decide which one:
- use the Parser options as @honestegg mentioned
- wrap all your users' code into a function like I already explained in another comment
- fork the source and make the defaults you want
I might change my mind as I often do if I am given convincing arguments, but right now I won't change the default behavior.
from jint.
Related Issues (20)
- Tasks cannot be executed sequentially
- Max Statements and general recommendations for limits HOT 1
- JsonValue primitives HOT 1
- The type initializer for 'Jint.Native.Global.GlobalObject' threw an exception.
- Unexpected token using modules HOT 1
- Can no longer use cached Esprima Scripts HOT 1
- LocaleCompare encountered inconsistent results with the browser HOT 1
- NRE after upgrading to Jint 3.1.1 from 3.1.0 HOT 1
- URL API HOT 1
- Wrong Repo, sorry HOT 1
- Task<JsValue> is not correctly marshalled into JS code HOT 2
- Implicit Namespaces & Interopt HOT 4
- Addition-assignment -, subtract-assignment - and multiply-assignment operator throws an error when using BigInt HOT 2
- Incorrect C# code not throwing an exception HOT 1
- Engine.Execute fails when executing Array.map (d3.js) HOT 1
- System.NullReferenceException: Object reference not set to an instance of an object. HOT 5
- Setting private properties causes NRE HOT 6
- Is the tail-call feature really needed?
- Accessing dynamic objects returns incorrect type HOT 1
- reference.ReferencedName type is number but was string in older versions HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jint.