Replies: 4 comments
-
Can you try out what's the winning combination for your use case and submit a PR to update sample? |
Beta Was this translation helpful? Give feedback.
-
No problem, but I haven't figured out how to import a module yet~~ I have some Jint extension functions that C# delegates |
Beta Was this translation helpful? Give feedback.
-
this is my code public static Engine CreateJsEngine(this IScriptingManager scriptingManager,
IServiceProvider serviceProvider,
IEnumerable<GlobalMethod> extraMethods = null)
{
var siteService = serviceProvider.GetRequiredService<ISiteService>();
var siteSettings = siteService.GetSiteSettingsAsync().Result;
var siteTimeZone = siteSettings.TimeZoneId != null ? TZConvert.GetTimeZoneInfo(siteSettings.TimeZoneId) : TimeZoneInfo.Local;
var engine = new Engine(options =>
{
options.AddExtensionMethods(typeof(FreeSQLExtensions));
options.TimeZone = siteTimeZone;
options.LocalTimeZone(siteTimeZone);
options.Interop.MemberAccessor = static (_, target, member) =>
{
if (target is JArray && !int.TryParse(member, out var _))
{
return JsValue.Undefined;
}
return null;
};
});
var methods = scriptingManager.GlobalMethodProviders.SelectMany(x => x.GetMethods());
if (extraMethods != null)
{
methods = methods.Concat(extraMethods);
}
foreach (var method in methods)
{
engine.SetValue(method.Name, method.Method(serviceProvider));
}
//formatDate is a C# delegate ,it was registed with above code
engine.AddModule("dateHelper", @"
export const dateHelper={
formatDate
};
");
engine.ImportModule("dateHelper");
//call : return dateHelper.formatDate(new Date(), 'yyyy-MM-dd')
//-> Jint.Runtime.JavaScriptException: dateHelper is not defined\r\n ---> Error: dateHelper is not defined\r\n at <anonymous>:1:26
return engine;
} input
output
|
Beta Was this translation helpful? Give feedback.
-
I'm looking at the unit tests [Fact]
public void CanReuseModule()
{
const string Code = "export function formatName(firstName, lastName) {\r\n return `${firstName} ${lastName}`;\r\n}";
var module = Engine.PrepareModule(Code);
for (var i = 0; i < 5; i++)
{
var engine = new Engine();
engine.AddModule("__main__", x => x.AddModule(module));
var ns = engine.ImportModule("__main__");
var result = engine.Invoke(ns.Get("formatName"), "John" + i, "Doe").AsString();
Assert.Equal($"John{i} Doe", result);
}
} Do we have to access imported modules this way? var ns = engine.ImportModule("__main__");
var result = engine.Invoke(ns.Get("formatName"), "John" + i, "Doe").AsString(); and not support like this? engine.Evaluate(" return dateHelper.formatDate(new Date(), 'yyyy-MM-dd') ").ToObject(); |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello, I tried to create a module with the following code according to the instructions on the home page, but this method no longer exists, should I replace it with
AddModule
now?Beta Was this translation helpful? Give feedback.
All reactions