Calling class method
#1805
-
Hi all, I'm trying to determine if I'm doing something wrong - I'm piecing this together by looking at the tests. I can have multiple files, which all export a class called The JS looks something like: export class Rule {
meta = { ... }
evaluate(param1, param2) {
return 0
}
} And the code is: engine.Modules.Add(name, script);
var jsRuleClass = engine.Modules.Import(name).Get("Rule");
var instance = engine.Construct(jsRuleClass);
// This works:
value = (JsObject) instance.GetProperty("meta").Value; However, GetMethod() doesn't, it says "Cannot access internal method". I guess the test is able to access the internal methods. Am I going about this the wrong way? |
Beta Was this translation helpful? Give feedback.
Answered by
lahma
Mar 16, 2024
Replies: 1 comment 5 replies
-
Here's an example how you would call the method with correct [Fact]
public void ShouldAllowInvokeUserDefinedClass()
{
_engine.Modules.Add("user", "export class UserDefined { constructor(v) { this._v = v; } hello(c) { return `hello ${this._v}${c}`; } }");
var ctor = _engine.Modules.Import("user").Get("UserDefined");
var instance = _engine.Construct(ctor, "world");
var result = instance.Get("hello").Call(instance, new JsValue[] { "!" });
Assert.Equal("hello world!", result);
} |
Beta Was this translation helpful? Give feedback.
5 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Looks like you are passing instance as first argument instead or thisObj, see my example with first parameter as this and separate args array.