Quantcast
Channel: ClearScript
Viewing all articles
Browse latest Browse all 2297

Commented Unassigned: Exception when creating Dates in V8ScriptEngine [113]

$
0
0
Hello,

I'm trying to create/parse date from a JS evaluate and return them as strings back into c# code as such:

```
using (var engine = new V8ScriptEngine())
{
resultEval = engine.Evaluate(expression);
}
```

where my `expression` is

`new Date()`

and it return an "Microsoft.ClearScript.V8.V8ScriptItem" object with an "UnderlyingSystemType" saying:

'((Microsoft.ClearScript.ScriptItem)resultEval).UnderlyingSystemType' threw an exception of type 'System.NotImplementedException'

Now, I feel like this *should* work, but it's not. Any ideas? I don't think anything went wrong during the build/reference process. Other JS libraries such as Math work flawlessly.

Also, this is only an issue when trying to create a `new` date. Calling `Date()` works, but it only displays today's date.
Comments: Hello, Thanks a lot for the help. > Now, when a Date or any other script object is returned to the host, ClearScript wraps it in a dynamic proxy that provides full access to the script object. This was the missing piece. Now, my code works: ``` dynamic scriptVal = engine.Evaluate(expression); //Cannot directly check for V8ScriptItem type because it is protected. if (scriptVal.GetType().FullName == "Microsoft.ClearScript.V8.V8ScriptItem") { DateTime tempDate; if(DateTime.TryParse(scriptVal.toISOString(), out tempDate)) { resultEval = tempDate; } else { resultEval = ""; } } ``` But I still have two questions: 1) I can't seem to use V8ScriptItem for type-checking because it is protected. Is this intended? I found the workaround of using the GetType().FullName to accomplish what I want 2) As you can see, right now I'm checking to see if it's a V8ScriptItem in order to try to parse and extract the date. To explain my intent. I want the user to be able to return `strings`, `dates` and `numeric` datatypes without having to specify a specific variable (such as jsNow in your example) note: This is why I think `Evaluate` seems more appropriate than `Execute` based on my tests. e.g. their expression (js) could be: ``` if (true){ Math.floor(2.345); } else{ Math.floor(3.456); } ``` result: 2 ``` new Date("March 21, 2016") ``` result: 2016-03-21T00:00:00Z ``` "hello" ``` result: "hello" I guess my question is, with that intent in mind, are there other scenarios where the ScriptEngine returns a V8ScriptItem that isn't a js Date, that I should take in consideration?

Viewing all articles
Browse latest Browse all 2297

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>