Return new object instance from Uno

I’m trying to return a NativeModule to JS like this:

public NativeModule NewLogger (Context c, object[] args) {
  return new NativeModule(
    new NativeFunction("log", (NativeCallback)Log),
  );
}

But there seems to be no support for this. Is this planned?

Not planned, but it is a good idea. Filed a ticket.

Upon further investigation, this is how you do it:

var module = new NativeModule(new NativeFunction("log", (NativeCallback)Log));
return module.Evaluate("Logger", c);

It works!

This functionality is now removed. How can one do it now?

This is the old Evaluate:

public object Evaluate(string id, Context c)
{
    var obj = (Scripting.Object)c.Evaluate(id, "new Object()");

    foreach (var m in _members)
        m.Create(obj, c);

    _isEvaluated = true;

    var handler = _evaluated;
    if (handler!=null)
    {
        handler(null, EventArgs.Empty);
        _evaluated = null;
    }

    return obj;
}

This is the new one:

public void Evaluate(Context c, Scripting.Object module)
{
    var obj = module["exports"] as Scripting.Object;

    foreach (var m in _members)
        m.Create(obj, c);

    _isEvaluated = true;

    var handler = _evaluated;
    if (handler != null)
    {
        handler(null, EventArgs.Empty);
        _evaluated = null;
    }
}

I tried to use the new one:

Fuse.Scripting.Object obj = c.NewObject();
module.Evaluate(c, obj);
return obj;

Preview detected an unhandled exception: System.AggregateException: One or more errors occurred. ---> Fuse.Scripting.ScriptException: Name: System.NullReferenceException: Object reference not set to an instance of an object
  at Fuse.Scripting.NativeMember.Create (Fuse.Scripting.Object obj, Fuse.Scripting.Context context) [0x00000] in <filename unknown>:0
  at Fuse.Scripting.NativeModule.Evaluate (Fuse.Scripting.Context c, Fuse.Scripting.Object module) [0x00000] in <filename unknown>:0
  at JSFactory.NewLogger (Fuse.Scripting.Context c, System.Object[] args) [0x00000] in <filename unknown>:0
  at Fuse.Scripting.NativeFunction+NativeFunctionClosure.Callback (System.Object[] args) [0x00000] in <filename unknown>:0
  at Fuse.Scripting.V8.Marshaller+CallbackWrapper.Call (Fuse.Scripting.V8.Simple.UniqueValueVector args) [0x00000] in <filename unknown>:0
Error message: Uncaught System.NullReferenceException: Object reference not set to an instance of an object
  at Fuse.Scripting.NativeMember.Create (Fuse.Scripting.Object obj, Fuse.Scripting.Context context) [0x00000] in <filename unknown>:0
  at Fuse.Scripting.NativeModule.Evaluate (Fuse.Scripting.Context c, Fuse.Scripting.Object module) [0x00000] in <filename unknown>:0
  at JSFactory.NewLogger (Fuse.Scripting.Context c, System.Object[] args) [0x00000] in <filename unknown>:0
  at Fuse.Scripting.NativeFunction+NativeFunctionClosure.Callback (System.Object[] args) [0x00000] in <filename unknown>:0
  at Fuse.Scripting.V8.Marshaller+CallbackWrapper.Call (Fuse.Scripting.V8.Simple.UniqueValueVector args) [0x00000] in <filename unknown>:0
File name: MainView.ux
Line number: 5
Source line:         var log1 = Factory.newLogger("Hello 1");

I tried to copy the old Evaluate:

Logger.uno(23.21): E4040: 'Fuse.Scripting.NativeModule._members' is not accessible from 'Logger.Evaluate(string,Fuse.Scripting.Context)'
Logger.uno(24.4): E4040: 'Fuse.Scripting.NativeMember.Create(Fuse.Scripting.Object,Fuse.Scripting.Context)' is not accessible from 'Logger.Evaluate(string,Fuse.Scripting.Context)'
Logger.uno(26.3): E4040: 'Fuse.Scripting.NativeModule._isEvaluated' is not accessible from 'Logger.Evaluate(string,Fuse.Scripting.Context)'
Logger.uno(28.17): E4040: 'Fuse.Scripting.NativeModule._evaluated' is not accessible from 'Logger.Evaluate(string,Fuse.Scripting.Context)'
Logger.uno(32.4): E4040: 'Fuse.Scripting.NativeModule._evaluated' is not accessible from 'Logger.Evaluate(string,Fuse.Scripting.Context)'

I tried to convert it to an IModule:

Logger.uno(24.4): E4040: 'Fuse.Scripting.NativeMember.Create(Fuse.Scripting.Object,Fuse.Scripting.Context)' is not accessible from 'Logger.Evaluate(string,Fuse.Scripting.Context)'
Logger.uno(43.4): E4040: 'Fuse.Scripting.NativeMember.Create(Fuse.Scripting.Object,Fuse.Scripting.Context)' is not accessible from 'Logger.Evaluate(Fuse.Scripting.Context,Fuse.Scripting.Object)'

But such attempts are as usual thwarted by the strict permissions of the internal fuse library.

This works!

var module = c.NewObject();
module["id"] = "Logger";
module["exports"] = c.NewObject();

var log = new Logger(s);
log.Evaluate(c, module);
return module["exports"];