|
| 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 |
0 commit comments