Skip to content

Commit 633363e

Browse files
committed
add bindings for yantra and jurassic
1 parent 831b7d4 commit 633363e

File tree

11 files changed

+356
-38
lines changed

11 files changed

+356
-38
lines changed

Runtime/ReactUnity.asmdef

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323
"RU.System.Numerics.Vectors.dll",
2424
"RU.System.Runtime.CompilerServices.Unsafe.dll",
2525
"RU.System.Memory.dll",
26-
"Newtonsoft.Json.dll"
26+
"Newtonsoft.Json.dll",
27+
"YantraJS.Core.dll",
28+
"Jurassic.dll"
2729
],
2830
"autoReferenced": true,
2931
"defineConstraints": [],
@@ -52,6 +54,16 @@
5254
"name": "Unity",
5355
"expression": "2021.2.0a0",
5456
"define": "REACT_TEXTCORE"
57+
},
58+
{
59+
"name": "Unity",
60+
"expression": "",
61+
"define": "REACT_DISABLE_JURASSIC"
62+
},
63+
{
64+
"name": "Unity",
65+
"expression": "",
66+
"define": "REACT_DISABLE_YANTRA"
5567
}
5668
],
5769
"noEngineReferences": false

Runtime/Scripting/ClearScriptEngine.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,11 @@ public void SetValue<T>(string key, T value)
157157
else Engine.AddHostObject(key, value);
158158
}
159159

160+
public void ClearValue(string key)
161+
{
162+
Engine.Execute("delete " + key);
163+
}
164+
160165
public object CreateTypeReference(Type type)
161166
{
162167
Engine.AddHostType(tempKey, type);

Runtime/Scripting/DomProxies/Console.cs

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -40,55 +40,31 @@ private void GenericLog(object msg, Action<string> baseCaller, params object[] s
4040
baseCaller(aStringBuilder.ToString());
4141
}
4242

43-
public void log(object msg)
44-
{
45-
GenericLog(msg, Debug.Log);
46-
}
4743
public void log(object msg, params object[] subs)
4844
{
4945
GenericLog(msg, Debug.Log, subs);
5046
}
5147

52-
public void info(object msg)
53-
{
54-
GenericLog(msg, Debug.Log);
55-
}
5648
public void info(object msg, params object[] subs)
5749
{
5850
GenericLog(msg, Debug.Log, subs);
5951
}
6052

61-
public void debug(object msg)
62-
{
63-
GenericLog(msg, Debug.Log);
64-
}
6553
public void debug(object msg, params object[] subs)
6654
{
6755
GenericLog(msg, Debug.Log, subs);
6856
}
6957

70-
public void warn(object msg)
71-
{
72-
GenericLog(msg, Debug.LogWarning);
73-
}
7458
public void warn(object msg, params object[] subs)
7559
{
7660
GenericLog(msg, Debug.LogWarning, subs);
7761
}
7862

79-
public void error(object msg)
80-
{
81-
GenericLog(msg, Debug.LogError);
82-
}
8363
public void error(object msg, params object[] subs)
8464
{
8565
GenericLog(msg, Debug.LogError, subs);
8666
}
8767

88-
public void dir(object msg)
89-
{
90-
GenericLog(msg, Debug.Log);
91-
}
9268
public void dir(object msg, params object[] subs)
9369
{
9470
GenericLog(msg, Debug.Log, subs);

Runtime/Scripting/IJavaScriptEngine.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@
66
#define REACT_JINT
77
#endif
88

9+
#if !REACT_DISABLE_YANTRA
10+
#define REACT_YANTRA
11+
#endif
12+
13+
#if !REACT_DISABLE_JURASSIC
14+
#define REACT_JURASSIC
15+
#endif
16+
917
using System;
1018
using System.Collections.Generic;
1119
using System.Reflection;
@@ -25,6 +33,20 @@ public enum JavascriptEngineType
2533
[UnityEngine.InspectorName("ClearScript (Disabled)")]
2634
#endif
2735
ClearScript = 2,
36+
37+
#if REACT_YANTRA
38+
#if !REACT_YANTRA
39+
[UnityEngine.InspectorName("Yantra (Disabled)")]
40+
#endif
41+
Yantra = 3,
42+
#endif
43+
44+
#if REACT_JURASSIC
45+
#if !REACT_JURASSIC
46+
[UnityEngine.InspectorName("Jurassic (Disabled)")]
47+
#endif
48+
Jurassic = 4,
49+
#endif
2850
}
2951

3052
public interface IJavaScriptEngine : IDisposable
@@ -35,6 +57,7 @@ public interface IJavaScriptEngine : IDisposable
3557
Exception TryExecute(string code, string fileName = null);
3658
object Evaluate(string code, string fileName = null);
3759
void SetValue<T>(string key, T value);
60+
void ClearValue(string key);
3861
object GetValue(string key);
3962
object CreateNativeObject(Dictionary<string, object> props);
4063
void SetProperty<T>(object obj, string key, T value);

Runtime/Scripting/JavascriptEngineHelpers.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@
66
#define REACT_JINT
77
#endif
88

9+
#if !REACT_DISABLE_YANTRA
10+
#define REACT_YANTRA
11+
#endif
12+
13+
#if !REACT_DISABLE_JURASSIC
14+
#define REACT_JURASSIC
15+
#endif
16+
917
namespace ReactUnity.Scripting
1018
{
1119
internal class JavascriptEngineHelpers
@@ -18,6 +26,14 @@ public static IJavaScriptEngineFactory GetEngineFactory(JavascriptEngineType typ
1826
case JavascriptEngineType.Jint:
1927
return new JintEngineFactory();
2028
#endif
29+
#if REACT_YANTRA
30+
case JavascriptEngineType.Yantra:
31+
return new YantraEngineFactory();
32+
#endif
33+
#if REACT_JURASSIC
34+
case JavascriptEngineType.Jurassic:
35+
return new JurassicEngineFactory();
36+
#endif
2137
#if REACT_CLEARSCRIPT
2238
case JavascriptEngineType.Auto:
2339
case JavascriptEngineType.ClearScript:
@@ -28,6 +44,10 @@ public static IJavaScriptEngineFactory GetEngineFactory(JavascriptEngineType typ
2844
return new JintEngineFactory();
2945
#elif REACT_CLEARSCRIPT
3046
return new ClearScriptEngineFactory();
47+
#elif REACT_YANTRA
48+
return new YantraEngineFactory();
49+
#elif REACT_JURASSIC
50+
return new JurassicEngineFactory();
3151
#else
3252
throw new System.Exception("Could not find a valid scripting engine.");
3353
#endif

Runtime/Scripting/JintEngine.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,11 @@ public void SetValue<T>(string key, T value)
110110
Engine.SetValue(key, CreateValue(value));
111111
}
112112

113+
public void ClearValue(string key)
114+
{
115+
Engine.SetValue(key, JsValue.Undefined);
116+
}
117+
113118
private JsValue CreateValue(object value)
114119
{
115120
if (value is Type t) return TypeReference.CreateTypeReference(Engine, t);
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
#if !REACT_DISABLE_JURASSIC
2+
#define REACT_JURASSIC
3+
#endif
4+
5+
#if REACT_JURASSIC
6+
using System;
7+
using System.Collections.Generic;
8+
using System.Reflection;
9+
using UnityEngine;
10+
using Jurassic;
11+
using Jurassic.Library;
12+
13+
namespace ReactUnity.Scripting
14+
{
15+
public class JurassicEngine : IJavaScriptEngine
16+
{
17+
public string Key { get; } = "yantra";
18+
public ScriptEngine Engine { get; }
19+
public object NativeEngine => Engine;
20+
21+
public JurassicEngine(ReactContext context, bool debug, bool awaitDebugger)
22+
{
23+
Engine = new ScriptEngine();
24+
Engine.EnableExposedClrTypes = true;
25+
Engine.EnableILAnalysis = true;
26+
}
27+
28+
public object Evaluate(string code, string fileName = null)
29+
{
30+
return Engine.Evaluate(code);
31+
}
32+
33+
public void Execute(string code, string fileName = null)
34+
{
35+
Engine.Evaluate(code);
36+
}
37+
38+
public Exception TryExecute(string code, string fileName = null)
39+
{
40+
try
41+
{
42+
Execute(code, fileName);
43+
}
44+
catch (Exception ex)
45+
{
46+
Debug.LogException(ex);
47+
return ex;
48+
}
49+
return null;
50+
}
51+
52+
public object GetValue(string key)
53+
{
54+
return Engine.GetGlobalValue(key);
55+
}
56+
57+
public void SetProperty<T>(object obj, string key, T value)
58+
{
59+
if (obj is ObjectInstance so)
60+
so.SetPropertyValue(key, CreateValue(value), false);
61+
}
62+
63+
public void SetValue<T>(string key, T value)
64+
{
65+
Engine.SetGlobalValue(key, CreateValue(value));
66+
}
67+
68+
public void ClearValue(string key)
69+
{
70+
Engine.SetGlobalValue(key, Undefined.Value);
71+
}
72+
73+
private object CreateValue(object value)
74+
{
75+
return value;
76+
}
77+
78+
public object CreateTypeReference(Type type)
79+
{
80+
return type;
81+
}
82+
83+
public object CreateNamespaceReference(string ns, params Assembly[] assemblies)
84+
{
85+
return Undefined.Value;
86+
}
87+
88+
public object CreateNativeObject(Dictionary<string, object> props)
89+
{
90+
return props;
91+
}
92+
93+
public void Dispose()
94+
{
95+
}
96+
97+
public IEnumerator<KeyValuePair<string, object>> TraverseScriptObject(object obj)
98+
{
99+
if (obj is ObjectInstance jv)
100+
{
101+
var keys = jv.Properties;
102+
var en = keys.GetEnumerator();
103+
104+
while (en.MoveNext())
105+
{
106+
var prop = en.Current;
107+
yield return new KeyValuePair<string, object>(prop.Key.ToString(), prop.Value);
108+
}
109+
}
110+
else if (obj is IEnumerable<KeyValuePair<string, object>> eo)
111+
{
112+
foreach (var kv in eo) yield return kv;
113+
}
114+
}
115+
}
116+
117+
public class JurassicEngineFactory : IJavaScriptEngineFactory
118+
{
119+
public JavascriptEngineType EngineType => JavascriptEngineType.Jurassic;
120+
121+
public IJavaScriptEngine Create(ReactContext context, bool debug, bool awaitDebugger)
122+
{
123+
return new JurassicEngine(context, debug, awaitDebugger);
124+
}
125+
}
126+
}
127+
#endif

Runtime/Scripting/JurassicEngine.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Runtime/Scripting/ScriptContext.cs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ public Callback CreateEventCallback(string code, object thisVal)
8989
{
9090
Engine.SetValue("__thisArg", thisVal);
9191
var fn = EvaluateScript(
92-
"(function(ts) { delete __thisArg; return (function(event, sender) {\n" + code + "\n}).bind(ts); })(__thisArg)");
92+
"(function(ts) { return (function(event, sender) {\n" + code + "\n}).bind(ts); })(__thisArg)");
93+
Engine.ClearValue("__thisArg");
9394

9495
return Callback.From(fn, Context, thisVal);
9596
}
@@ -115,20 +116,20 @@ void CreateConsole(IJavaScriptEngine engine)
115116
engine.Execute(@"(function() {
116117
var _console = __console;
117118
console = {
118-
log: (...args) => { _console.log(...args) },
119-
info: (...args) => { _console.info(...args) },
120-
debug: (...args) => { _console.debug(...args) },
121-
trace: (...args) => { _console.debug(...args) },
122-
warn: (...args) => { _console.warn(...args) },
123-
error: (...args) => { _console.error(...args) },
124-
exception: (...args) => { _console.exception(...args) },
125-
dir: (...args) => { _console.dir(...args) },
126-
clear: (...args) => { _console.clear(...args) },
127-
assert: (...args) => { _console.assert(...args) },
128-
count: (name) => _console.count(name),
119+
log: function log (arg) { _console.log(arg) },
120+
info: function info (arg) { _console.info(arg) },
121+
debug: function debug (arg) { _console.debug(arg) },
122+
trace: function trace (arg) { _console.debug(arg) },
123+
warn: function warn (arg) { _console.warn(arg) },
124+
error: function error (arg) { _console.error(arg) },
125+
exception: function exception (arg) { _console.exception(arg) },
126+
dir: function dir (arg) { _console.dir(arg) },
127+
clear: function clear (arg) { _console.clear(arg) },
128+
assert: function assert (arg) { _console.assert(arg) },
129+
count: function count (name) { return _console.count(name) },
129130
};
130-
delete __console;
131131
})()");
132+
engine.ClearValue("__console");
132133
}
133134

134135
void CreatePolyfills(IJavaScriptEngine engine)

0 commit comments

Comments
 (0)