koculu / Topaz

Multithreaded Javascript Engine for .NET
MIT License
222 stars 13 forks source link

[Feature Request] Followup to 18: hasOwnProperty #20

Closed warappa closed 11 months ago

warappa commented 11 months ago

Describe the bug The example I had shown you in #18 works, but hasOwnProperty is still missing functionality. In JavaScript, hasOwnProperty works on any object, no matter what it is.

Expected behavior hasOwnProperty should work for any object, regardless if it implements ConcurrentJsObject.cs or IDictionary or something else. To do this it may even utilize reflection if it has to.

Reproduction Given a class like

public class TestDto
{
    public int SomeProperty { get; set; }
}

running the following code should return true

var engine = new TopazEngine();
var someObject = new TestDto { SomeProperty = 123 };

engine.SetValue("ob", someObject);

var result = engine.ExecuteExpression("ob.hasOwnProperty('SomeProperty')");
Console.WriteLine(result);

Desktop

koculu commented 11 months ago

Thanks @warappa for reporting this issue. My point of view:

void TestExtensionMethod() { var engine = new TopazEngine(); engine.AddExtensionMethods(typeof(TopazObjectExtensions)); var someObject = new TestDto { SomeProperty = 123 }; engine.SetValue("someObject", someObject); var result = engine.ExecuteExpression("someObject.hasOwnProperty('SomeProperty')"); Console.WriteLine(result); }

warappa commented 11 months ago

Sorry, I see I should have picked feature request.

Thank you for the example of the alternative way, I will try it.

Topaz does not enforce full Javascript API compatibility

This is an important point for me to consider as I would have to mimic the JavaScript API as far as we use it.

koculu commented 11 months ago

Topaz does as few as possible to keep the engine simple and fast and provides extension points. Please consider that not all consumers would need fully capable Javascript API.

For all extension opportunities, please refer to TopazEngineSetup

koculu commented 11 months ago

The dotnet CLR objects exposed in Topaz Runtime are not JavaScript objects and by default they don't implement standard JavaScript object methods.

The JsObject is designed to mimic Javascript Objects and implements a few essential JavaScript object methods.

Users are responsible for providing methods callable by the JavaScript runtime for the exposed CLR objects.

I am closing this feature request because there is a viable method to achieve the desired functionality.