private string JSValueToString(JavaScriptValue val) { switch (val.ValueType) { case JavaScriptValueType.Boolean: return(val.ToBoolean() ? "true" : "false"); case JavaScriptValueType.Number: try { return(val.ToInt32().ToString()); } catch (JavaScriptException) { return(val.ToDouble().ToString(CultureInfo.InvariantCulture)); } case JavaScriptValueType.Error: var keys = val.GetOwnPropertyNames(); throw new Exception("Error in js"); case JavaScriptValueType.String: return(val.ToString()); case JavaScriptValueType.Array: // int length = val.GetProperty(JavaScriptPropertyId.FromString("length")).ToInt32(); // return "Array"; case JavaScriptValueType.Object: return(val.ToJsonString()); default: return(val.ValueType.ToString()); } }
private object Visit(JavaScriptValue value, IShibaContext context) { switch (value.ValueType) { case JavaScriptValueType.Error: case JavaScriptValueType.Object: return(VisitJavascriptObject(value)); case JavaScriptValueType.Array: return(VisitJavascriptArray(value)); case JavaScriptValueType.Boolean: return(value.ToBoolean()); case JavaScriptValueType.Null: return(null); case JavaScriptValueType.Number: return(value.ToDouble()); case JavaScriptValueType.String: return(value.ToString()); case JavaScriptValueType.Undefined: case JavaScriptValueType.Function: default: return(value); } }
private JToken VisitNumber(JavaScriptValue value) { var number = value.ToDouble(); return(number % 1 == 0 ? new JValue((long)number) : new JValue(number)); }
private JToken VisitNumber(JavaScriptValue value) { var number = value.ToDouble(); return number % 1 == 0 ? new JValue((long)number) : new JValue(number); }
private IEnumerable <Span> GetValueSpans(JavaScriptValue value, int level = 0, bool inArray = false) { switch (value.ValueType) { case JavaScriptValueType.Null: return(new Span[] { new Span("null") }); case JavaScriptValueType.Undefined: return(new Span[] { new Span("undefined") }); case JavaScriptValueType.String: return(new Span[] { $"\"{value.ToString().Replace("\"", "\\\"")}\"".DarkGreen() }); case JavaScriptValueType.Boolean: return(new Span[] { new Span(value.ToBoolean() ? "true" : "false") }); case JavaScriptValueType.Number: try { return(new Span[] { value.ToInt32().ToString().Red() }); } catch (JavaScriptException) { return(new Span[] { value.ToDouble().ToString(CultureInfo.InvariantCulture).Red() }); } case JavaScriptValueType.Function: return(new Span[] { GetFuncRep(value).Magenta() }); case JavaScriptValueType.Error: return(new Span[] { value.ToJsonString().DarkRed() }); case JavaScriptValueType.Object: return(GetObjectSpans(value, level, inArray)); case JavaScriptValueType.Array: return(GetArraySpans(value, level)); default: return(new Span[] { new Span(value.ValueType.ToString()), }); } }
// // The main entry point for the host. // public static int Main(string[] arguments) { int returnValue = 1; CommandLineArguments commandLineArguments; commandLineArguments.ArgumentsStart = 0; if (arguments.Length - commandLineArguments.ArgumentsStart < 1) { Console.Error.WriteLine("usage: chakrahost <script name> <arguments>"); return(returnValue); } try { // // Create the runtime. We're only going to use one runtime for this host. // using (JavaScriptRuntime runtime = JavaScriptRuntime.Create()) { // // Similarly, create a single execution context. Note that we're putting it on the stack here, // so it will stay alive through the entire run. // JavaScriptContext context = CreateHostContext(runtime, arguments, commandLineArguments.ArgumentsStart); // // Now set the execution context as being the current one on this thread. // using (new JavaScriptContext.Scope(context)) { // // Load the script from the disk. // string script = File.ReadAllText(arguments[commandLineArguments.ArgumentsStart]); // // Run the script. // JavaScriptValue result; try { result = JavaScriptContext.RunScript(script, currentSourceContext++, arguments[commandLineArguments.ArgumentsStart]); } catch (JavaScriptScriptException e) { PrintScriptException(e.Error); return(1); } catch (Exception e) { Console.Error.WriteLine("chakrahost: failed to run script: {0}", e.Message); return(1); } // // Convert the return value. // JavaScriptValue numberResult = result.ConvertToNumber(); double doubleResult = numberResult.ToDouble(); returnValue = (int)doubleResult; } } } catch (Exception e) { Console.Error.WriteLine("chakrahost: fatal error: internal error: {0}.", e.Message); } Console.WriteLine(returnValue); return(returnValue); }
/// <summary> /// Converts a <see cref="JavaScriptValue"/> with a number type to host value. /// </summary> public object ToHostNumber(JavaScriptValue arg, Type toType) { // 32-bit Conversions if (toType == typeof(byte)) { return((byte)arg.ToInt32()); } if (toType == typeof(sbyte)) { return((sbyte)arg.ToInt32()); } if (toType == typeof(short)) { return((short)arg.ToInt32()); } if (toType == typeof(ushort)) { return((ushort)arg.ToInt32()); } if (toType == typeof(int)) { return(arg.ToInt32()); } if (toType == typeof(uint)) { return((uint)arg.ToInt32()); } // 64-bit Conversions if (toType == typeof(long)) { return((long)arg.ToDouble()); } if (toType == typeof(ulong)) { return((ulong)arg.ToDouble()); } if (toType == typeof(float)) { return((float)arg.ToDouble()); } if (toType == typeof(double)) { return(arg.ToDouble()); } if (toType == typeof(decimal)) { return((decimal)arg.ToDouble()); } // Other Conversions if (toType == typeof(string)) { return(arg.ConvertToString().ToString()); } if (toType == typeof(bool)) { return(arg.ConvertToBoolean().ToBoolean()); } // Last Attempt if (toType.IsAssignableFrom(typeof(double))) { return(arg.ToDouble()); } throw new Exception($"Cannot convert javascript number to type: {toType}"); }
private static object ConvertJavaScriptValue(JavaScriptValue value) { JavaScriptValueType t = value.ValueType; if (t == JavaScriptValueType.Boolean) { return(value.ToBoolean()); } else if (t == JavaScriptValueType.Number) { return(value.ToDouble()); } else if (t == JavaScriptValueType.String) { return(value.ToString()); } else if (t == JavaScriptValueType.Null) { return(null); } else if (t == JavaScriptValueType.Undefined) { return(null); } else if (t == JavaScriptValueType.Object) { Dictionary <string, object> dictionary = new Dictionary <string, object>(); JavaScriptValue propNames = value.GetOwnPropertyNames(); int i = 0; JavaScriptValue index; while (propNames.HasIndexedProperty(index = JavaScriptValue.FromInt32(i))) { JavaScriptValue propName = propNames.GetIndexedProperty(index); JavaScriptPropertyId propId = JavaScriptPropertyId.FromString(propName.ToString()); JavaScriptValue propValue = value.GetProperty(propId); object value2 = ConvertJavaScriptValue(propValue); dictionary.Add(propName.ToString(), value2); //Debug.WriteLine(propName.ToString() + ": " + propValue.ConvertToString().ToString()); i += 1; } return(dictionary); } else if (t == JavaScriptValueType.Array) { JavaScriptPropertyId lengthPropId = JavaScriptPropertyId.FromString("length"); JavaScriptValue arrayLength = value.GetProperty(lengthPropId); int length = (int)arrayLength.ToDouble(); object[] array = new object[length]; int i; JavaScriptValue index; for (i = 0; i < length; i++) { if (value.HasIndexedProperty(index = JavaScriptValue.FromInt32(i))) { JavaScriptValue prop = value.GetIndexedProperty(index); array[i] = ConvertJavaScriptValue(prop); } } return(array); } return(null); }