Skip to content

Returning native Javascript objects from CLR #37

Description

@jsobell

I have a situation where a registered library (e.g. AddHostObject("utils", new Utils())) has a function that returns an object based on a set of parameters. All is fine, so long as the type is a core system type marshalled to number, string, boolean etc, but if the return type is a DateTime object things get very messy.
I can modify my external call to return a long number, and require the user always wrap it in a Date(blah), but this is messy and unintuitive for users:
var x = utils.GetDate('tomorrow').getDay()
has to be coded as
var x = new Date(utils.GetDate('tomorrow')).getDay()

Looking at the returned types, the DotNet DateTime is returned as a HostObject, but a Javascript Date object is a V8ScriptItem.
Is there any way to have the result of a CLR function return a javascript Date object? I can't wrap the call in the expression because it may be used in the middle of another expression (e.g.
console.log(`Hi Dave, I can't do that until ${utils.GetDate('tomorrow').toLocalString()}`)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions