-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
CustomObject.cs
146 lines (119 loc) · 3.47 KB
/
CustomObject.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YantraJS.Core.Clr;
using YantraJS.Utils;
namespace YantraJS.Core.Tests.ClrObjects
{
public class CustomObject: JavaScriptObject
{
Dictionary<int, object> dictionary = new Dictionary<int, object>();
/// <summary>
/// Indexer must be exported
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
[JSExport]
public object this[int index]
{
get => dictionary.TryGetValue(index, out var v) ? v : null;
set => dictionary[index] = value;
}
/// <summary>
/// JavaScriptObject must have a constructor with in Arguments a
/// </summary>
/// <param name="a"></param>
public CustomObject(in Arguments a): base(a) {
this.Name = a[0]?.ToString() ?? "Not Specified";
this.Version = 4;
}
[JSExportSameName]
public static int NONE = 0;
[JSExport("name")]
public string Name { get; set; }
/// <summary>
/// Hidden from JavaScript world...
/// </summary>
public int Version { get; set; }
[JSExport]
public JSValue Print(in Arguments a)
{
return a[0] ?? JSNull.Value;
}
[JSExport]
public int Add(int a, int b)
{
return a + b;
}
[JSExport]
public void Log(string a)
{
System.Diagnostics.Debug.WriteLine(a);
}
/// <summary>
/// Hidden from JS
/// </summary>
/// <param name="a"></param>
/// <returns></returns>
public object Print(object a)
{
return a;
}
/// <summary>
/// Hidden from JS
/// </summary>
/// <param name="a"></param>
/// <returns></returns>
public object Print2(object a)
{
return a;
}
}
[TestClass]
public class CustomObjectTest
{
[TestMethod]
public void Test()
{
var c = new JSTestContext();
c["CustomObject"] = ClrType.From(typeof(CustomObject));
c.Eval(@"
var a = new CustomObject('b');
assert.strictEqual(a.name, 'b');
assert.strictEqual(a.version, undefined);
assert.strictEqual(a.print(2), 2);
assert.strictEqual(a.add(1,2), 3);
a.log('b');
assert.strictEqual(CustomObject.NONE,0);
assert.throws(() => {
a.print2(2);
});
a[1] = 'a';
assert.strictEqual(a[1],'a');
");
}
[TestMethod]
public void TestNamingConvention()
{
var c = new JSTestContext();
c.ClrMemberNamingConvention = ClrMemberNamingConvention.Declared;
c["CustomObject"] = ClrType.From(typeof(CustomObject2));
c.Eval(@"
var a = new CustomObject('b');
assert.strictEqual(a.Name, 'b');
assert.strictEqual(a.Version, undefined);
");
}
}
public class CustomObject2
{
public string Name { get; }
public CustomObject2(string a)
{
this.Name = a;
}
}
}