|
| 1 | +/* |
| 2 | + * Copyright (c) 2014, Facebook, Inc. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. An additional grant |
| 7 | + * of patent rights can be found in the PATENTS file in the same directory. |
| 8 | + */ |
| 9 | + |
| 10 | +using System; |
| 11 | +using System.Linq; |
| 12 | +using JavaScriptEngineSwitcher.Core; |
| 13 | +using Newtonsoft.Json; |
| 14 | + |
| 15 | +using OriginalJsEngine = VroomJs.JsEngine; |
| 16 | +using OriginalJsException = VroomJs.JsException; |
| 17 | + |
| 18 | +namespace React.JavaScriptEngine.VroomJs |
| 19 | +{ |
| 20 | + /// <summary> |
| 21 | + /// Basic implementation of a VroomJs JavaScript engine for JavaScriptEngineSwitcher. |
| 22 | + /// Connects to the V8 JavaScript engine on Linux and Mac OS X. |
| 23 | + /// </summary> |
| 24 | + public class VroomJsEngine : JsEngineBase |
| 25 | + { |
| 26 | + /// <summary> |
| 27 | + /// The VroomJs engine |
| 28 | + /// </summary> |
| 29 | + private readonly OriginalJsEngine _jsEngine; |
| 30 | + |
| 31 | + /// <summary> |
| 32 | + /// Name of JavaScript engine |
| 33 | + /// </summary> |
| 34 | + public override string Name |
| 35 | + { |
| 36 | + get { return "VroomJs JavaScript engine"; } |
| 37 | + } |
| 38 | + |
| 39 | + /// <summary> |
| 40 | + /// Version of original JavaScript engine |
| 41 | + /// </summary> |
| 42 | + public override string Version |
| 43 | + { |
| 44 | + get { return "86f8558d (Aug 17, 2013)"; } |
| 45 | + } |
| 46 | + |
| 47 | + /// <summary> |
| 48 | + /// Constructs instance of adapter for VroomJs |
| 49 | + /// </summary> |
| 50 | + public VroomJsEngine() |
| 51 | + { |
| 52 | + try |
| 53 | + { |
| 54 | + _jsEngine = new OriginalJsEngine(); |
| 55 | + } |
| 56 | + catch (Exception e) |
| 57 | + { |
| 58 | + |
| 59 | + throw new JsEngineLoadException( |
| 60 | + string.Format( |
| 61 | + JavaScriptEngineSwitcher.Core.Resources.Strings.Runtime_JsEngineNotLoaded, |
| 62 | + Name, |
| 63 | + e.Message |
| 64 | + ), |
| 65 | + Name, |
| 66 | + Version, |
| 67 | + e |
| 68 | + ); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + /// <summary> |
| 73 | + /// Converts a VroomJs exception into a <see cref="JsRuntimeException" />. |
| 74 | + /// </summary> |
| 75 | + /// <param name="jsException">VroomJs exception</param> |
| 76 | + /// <returns>JavaScriptEngineSwitcher exception</returns> |
| 77 | + private JsRuntimeException ConvertJavaScriptExceptionToJsRuntimeException( |
| 78 | + OriginalJsException jsException |
| 79 | + ) |
| 80 | + { |
| 81 | + dynamic nativeEx = jsException.NativeException; |
| 82 | + return new JsRuntimeException(nativeEx.stack, Name, Version) |
| 83 | + { |
| 84 | + Category = nativeEx.name, |
| 85 | + }; |
| 86 | + } |
| 87 | + |
| 88 | + /// <summary> |
| 89 | + /// Evaluates the specified JavaScript expression |
| 90 | + /// </summary> |
| 91 | + /// <param name="expression">Expression to evaluate</param> |
| 92 | + /// <returns>Result of the expression</returns> |
| 93 | + protected override object InnerEvaluate(string expression) |
| 94 | + { |
| 95 | + try |
| 96 | + { |
| 97 | + return _jsEngine.Execute(expression); |
| 98 | + } |
| 99 | + catch (OriginalJsException ex) |
| 100 | + { |
| 101 | + throw ConvertJavaScriptExceptionToJsRuntimeException(ex); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + /// <summary> |
| 106 | + /// Evaluates the specified JavaScript expression |
| 107 | + /// </summary> |
| 108 | + /// <param name="expression">Expression to evaluate</param> |
| 109 | + /// <typeparam name="T">Return type</typeparam> |
| 110 | + /// <returns>Result of the expression</returns> |
| 111 | + protected override T InnerEvaluate<T>(string expression) |
| 112 | + { |
| 113 | + return (T) InnerEvaluate(expression); |
| 114 | + } |
| 115 | + |
| 116 | + /// <summary> |
| 117 | + /// Executes the specified JavaScript code |
| 118 | + /// </summary> |
| 119 | + /// <param name="code">Code to execute</param> |
| 120 | + protected override void InnerExecute(string code) |
| 121 | + { |
| 122 | + try |
| 123 | + { |
| 124 | + _jsEngine.Execute(code); |
| 125 | + } |
| 126 | + catch (OriginalJsException ex) |
| 127 | + { |
| 128 | + throw ConvertJavaScriptExceptionToJsRuntimeException(ex); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + /// <summary> |
| 133 | + /// Calls the specified JavaScript function |
| 134 | + /// </summary> |
| 135 | + /// <param name="functionName">Function to call</param> |
| 136 | + /// <param name="args">Arguments to pass to function</param> |
| 137 | + /// <returns>Result of the function</returns> |
| 138 | + protected override object InnerCallFunction(string functionName, params object[] args) |
| 139 | + { |
| 140 | + var code = string.Format( |
| 141 | + "{0}({1})", |
| 142 | + functionName, |
| 143 | + string.Join(", ", args.Select(JsonConvert.SerializeObject)) |
| 144 | + ); |
| 145 | + |
| 146 | + try |
| 147 | + { |
| 148 | + return _jsEngine.Execute(code); |
| 149 | + } |
| 150 | + catch (OriginalJsException ex) |
| 151 | + { |
| 152 | + throw ConvertJavaScriptExceptionToJsRuntimeException(ex); |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + /// <summary> |
| 157 | + /// Calls the specified JavaScript function |
| 158 | + /// </summary> |
| 159 | + /// <param name="functionName">Function to call</param> |
| 160 | + /// <param name="args">Arguments to pass to function</param> |
| 161 | + /// <typeparam name="T">Return type of the function</typeparam> |
| 162 | + /// <returns>Result of the function</returns> |
| 163 | + protected override T InnerCallFunction<T>(string functionName, params object[] args) |
| 164 | + { |
| 165 | + return (T) InnerCallFunction(functionName, args); |
| 166 | + } |
| 167 | + |
| 168 | + /// <summary> |
| 169 | + /// Determines if the specified variable has been set |
| 170 | + /// </summary> |
| 171 | + /// <param name="variableName">Name of the variable</param> |
| 172 | + /// <returns><c>true</c> if the variable is defined</returns> |
| 173 | + protected override bool InnerHasVariable(string variableName) |
| 174 | + { |
| 175 | + var code = string.Format("typeof {0} !== 'undefined'", variableName); |
| 176 | + return InnerEvaluate<bool>(code); |
| 177 | + } |
| 178 | + |
| 179 | + /// <summary> |
| 180 | + /// Gets the value of the specified variable |
| 181 | + /// </summary> |
| 182 | + /// <param name="variableName">Variable to get value of</param> |
| 183 | + /// <returns>Value</returns> |
| 184 | + protected override object InnerGetVariableValue(string variableName) |
| 185 | + { |
| 186 | + try |
| 187 | + { |
| 188 | + return _jsEngine.GetVariable(variableName); |
| 189 | + } |
| 190 | + catch (OriginalJsException ex) |
| 191 | + { |
| 192 | + throw ConvertJavaScriptExceptionToJsRuntimeException(ex); |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + /// <summary> |
| 197 | + /// Gets the value of the specified variable |
| 198 | + /// </summary> |
| 199 | + /// <param name="variableName">Variable to get value of</param> |
| 200 | + /// <typeparam name="T">Type of the variable</typeparam> |
| 201 | + /// <returns>Value</returns> |
| 202 | + protected override T InnerGetVariableValue<T>(string variableName) |
| 203 | + { |
| 204 | + return (T)InnerGetVariableValue(variableName); |
| 205 | + } |
| 206 | + |
| 207 | + /// <summary> |
| 208 | + /// Sets the value of the specified variable |
| 209 | + /// </summary> |
| 210 | + /// <param name="variableName">Variable to get value of</param> |
| 211 | + /// <param name="value">New value to set</param> |
| 212 | + /// <returns>Value</returns> |
| 213 | + protected override void InnerSetVariableValue(string variableName, object value) |
| 214 | + { |
| 215 | + try |
| 216 | + { |
| 217 | + _jsEngine.SetVariable(variableName, value); |
| 218 | + } |
| 219 | + catch (OriginalJsException ex) |
| 220 | + { |
| 221 | + throw ConvertJavaScriptExceptionToJsRuntimeException(ex); |
| 222 | + } |
| 223 | + } |
| 224 | + |
| 225 | + /// <summary> |
| 226 | + /// Deletes a variable |
| 227 | + /// </summary> |
| 228 | + /// <param name="variableName">Variable to delete</param> |
| 229 | + protected override void InnerRemoveVariable(string variableName) |
| 230 | + { |
| 231 | + var code = string.Format("{0} = undefined", variableName); |
| 232 | + try |
| 233 | + { |
| 234 | + _jsEngine.Execute(code); |
| 235 | + } |
| 236 | + catch (OriginalJsException ex) |
| 237 | + { |
| 238 | + throw ConvertJavaScriptExceptionToJsRuntimeException(ex); |
| 239 | + } |
| 240 | + } |
| 241 | + |
| 242 | + /// <summary> |
| 243 | + /// Releases resources used by this engine. |
| 244 | + /// </summary> |
| 245 | + public override void Dispose() |
| 246 | + { |
| 247 | + if (!_disposed) |
| 248 | + { |
| 249 | + _jsEngine.Dispose(); |
| 250 | + _disposed = true; |
| 251 | + } |
| 252 | + } |
| 253 | + } |
| 254 | +} |
0 commit comments