forked from microsoft/node-api-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJSRuntimeContextExtensions.cs
70 lines (63 loc) · 3.2 KB
/
JSRuntimeContextExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using Microsoft.JavaScript.NodeApi.Interop;
using Microsoft.JavaScript.NodeApi.Runtime;
namespace Microsoft.JavaScript.NodeApi.DotNetHost;
/// <summary>
/// Extension methods to support importing JavaScript types at runtime.
/// </summary>
public static class JSRuntimeContextExtensions
{
/// <summary>
/// Imports a module or module property from JavaScript and converts it to an interface.
/// </summary>
/// <typeparam name="T">.NET type that the imported JS value will be marshalled to.</typeparam>
/// <param name="module">Name of the module being imported, or null to import a
/// global property. This is equivalent to the value provided to <c>import</c> or
/// <c>require()</c> in JavaScript. Required if <paramref name="property"/> is null.</param>
/// <param name="property">Name of a property on the module (or global), or null to import
/// the module object. Required if <paramref name="module"/> is null.</param>
/// <param name="marshaller">JS marshaller instance to use to convert the imported value
/// to a .NET type.</param>
/// <returns>The imported value, marshalled to the specified .NET type.</returns>
/// <exception cref="ArgumentNullException">Both <paramref cref="module" /> and
/// <paramref cref="property" /> are null.</exception>
public static T Import<T>(
this JSRuntimeContext runtimeContext,
string? module,
string? property,
bool esModule,
JSMarshaller marshaller)
{
if (marshaller == null) throw new ArgumentNullException(nameof(marshaller));
JSValue jsValue = runtimeContext.Import(module, property, esModule);
return marshaller.FromJS<T>(jsValue);
}
/// <summary>
/// Imports a module or module property from JavaScript and converts it to an interface.
/// </summary>
/// <typeparam name="T">.NET type that the imported JS value will be marshalled to.</typeparam>
/// <param name="module">Name of the module being imported, or null to import a
/// global property. This is equivalent to the value provided to <c>import</c> or
/// <c>require()</c> in JavaScript. Required if <paramref name="property"/> is null.</param>
/// <param name="property">Name of a property on the module (or global), or null to import
/// the module object. Required if <paramref name="module"/> is null.</param>
/// <param name="marshaller">JS marshaller instance to use to convert the imported value
/// to a .NET type.</param>
/// <returns>The imported value, marshalled to the specified .NET type.</returns>
/// <exception cref="ArgumentNullException">Both <paramref cref="module" /> and
/// <paramref cref="property" /> are null.</exception>
public static T Import<T>(
this NodeEmbeddingThreadRuntime nodejs,
string? module,
string? property,
bool esModule,
JSMarshaller marshaller)
{
if (marshaller == null) throw new ArgumentNullException(nameof(marshaller));
JSValueScope scope = nodejs;
return scope.RuntimeContext.Import<T>(module, property, esModule, marshaller);
}
// TODO: ImportAsync()
}