internal static JsException Create(JsConvert convert, JsError error) { string type = (string)convert.FromJsValue(error.Type); string resource = (string)convert.FromJsValue(error.Resource); string message = (string)convert.FromJsValue(error.Message); int line = error.Line; int column = error.Column + 1; // because zero based. JsObject nativeException = (JsObject)convert.FromJsValue(error.Exception); JsException exception; if (type == "SyntaxError") { exception = new JsSyntaxError(type, resource, message, line, column); } else { exception = new JsException(type, resource, message, line, column, nativeException); } return(exception); }
internal static JsException Create(JsConvert convert, IntPtr nativeErrorObject) { unsafe { JsError *jsErr = (JsError *)nativeErrorObject; string type = (string)convert.FromJsValuePtr((JsValue *)jsErr->type); string res = (string)convert.FromJsValuePtr((JsValue *)jsErr->resource); string msg = (string)convert.FromJsValuePtr((JsValue *)jsErr->message); JsObject nativeException = (JsObject)convert.FromJsValuePtr((JsValue *)jsErr->exception); JsException exception; if (type == "SyntaxError") { //from syntax error? exception = new JsSyntaxError(type, res, msg, jsErr->line, jsErr->column); } else { exception = new JsException(type, res, msg, jsErr->line, jsErr->column, nativeException); } return(exception); } }
/// <summary> /// convert from jsvalue to managed value /// </summary> /// <param name="v"></param> /// <returns></returns> public object FromJsValue(ref JsValue v) { #if DEBUG_TRACE_API Console.WriteLine("Converting Js value to .net"); #endif switch (v.Type) { case JsValueType.Empty: case JsValueType.Null: return(null); case JsValueType.Boolean: return(v.I32 != 0); case JsValueType.Integer: return(v.I32); case JsValueType.Index: return((UInt32)v.I64); case JsValueType.Number: return(v.Num); case JsValueType.String: return(Marshal.PtrToStringUni(v.Ptr)); case JsValueType.Date: /* * // The formula (v.num * 10000) + 621355968000000000L was taken from a StackOverflow * // question and should be OK. Then why do we need to compensate by -26748000000000L * // (a value determined from the failing tests)?! * return new DateTime((long)(v.Num * 10000) + 621355968000000000L - 26748000000000L); */ //var msFromJsTime = v.I64 % 1000; return(EPOCH_LocalTime.AddMilliseconds(v.I64)); case JsValueType.Array: { int len = v.I32; object[] newarr = new object[len]; unsafe { JsValue *arr = (JsValue *)v.Ptr; for (int i = 0; i < len; ++i) { newarr[i] = FromJsValuePtr((arr + i)); } } return(newarr); } case JsValueType.UnknownError: if (v.Ptr != IntPtr.Zero) { return(new JsException(Marshal.PtrToStringUni(v.Ptr))); } return(new JsInteropException("unknown error without reason")); case JsValueType.StringError: return(new JsException(Marshal.PtrToStringUni(v.Ptr))); case JsValueType.Managed: return(_context.KeepAliveGet(v.I32)); case JsValueType.JsTypeWrap: //auto unwrap return(_context.GetObjectProxy(v.I32).WrapObject); case JsValueType.ManagedError: Exception inner = _context.KeepAliveGet(v.I32) as Exception; string msg = null; if (v.Ptr != IntPtr.Zero) { msg = Marshal.PtrToStringUni(v.Ptr); } else if (inner != null) { msg = inner.Message; } return(new JsException(msg, inner)); case JsValueType.Dictionary: return(CreateJsDictionaryObject(ref v)); case JsValueType.Wrapped: return(new JsObject(_context, v.Ptr)); case JsValueType.Error: return(JsException.Create(this, v.Ptr)); case JsValueType.Function: //convert from js function delegate to managed //this compose of function ptr and delegate's target unsafe { JsValue *arr = (JsValue *)v.Ptr; return(new JsFunction(_context, (arr)->Ptr, (arr + 1)->Ptr)); } default: throw new InvalidOperationException("unknown type code: " + v.Type); } }
public object FromJsValue(JsValue v) { #if DEBUG_TRACE_API Console.WriteLine("Converting Js value to .net"); #endif switch (v.Type) { case JsValueType.Empty: case JsValueType.Null: return(null); case JsValueType.Boolean: return(v.I32 != 0); case JsValueType.Integer: return(v.I32); case JsValueType.Index: return((UInt32)v.I64); case JsValueType.Number: return(v.Num); case JsValueType.String: return(Marshal.PtrToStringUni(v.Ptr)); case JsValueType.Date: /* * // The formula (v.num * 10000) + 621355968000000000L was taken from a StackOverflow * // question and should be OK. Then why do we need to compensate by -26748000000000L * // (a value determined from the failing tests)?! * return new DateTime((long)(v.Num * 10000) + 621355968000000000L - 26748000000000L); */ //var msFromJsTime = v.I64 % 1000; return(EPOCH_LocalTime.AddMilliseconds(v.I64)); // + new TimeSpan(7, 0, 0); //return EPOCH_LocalTime.AddMilliseconds(v.I64);// + new TimeSpan(7, 0, 0); //return EPOCH.AddMilliseconds(v.I64); //return EPOCH.AddMilliseconds(v.Num); //return new DateTime((long)(v.Num * 10000) + 621355968000000000L - 26748000000000L); case JsValueType.Array: { int len = v.Length; var r = new object[len]; for (int i = 0; i < len; i++) { var vi = (JsValue)Marshal.PtrToStructure(new IntPtr(v.Ptr.ToInt64() + (16 * i)), typeof(JsValue)); r[i] = FromJsValue(vi); } return(r); } case JsValueType.UnknownError: if (v.Ptr != IntPtr.Zero) { return(new JsException(Marshal.PtrToStringUni(v.Ptr))); } return(new JsInteropException("unknown error without reason")); case JsValueType.StringError: return(new JsException(Marshal.PtrToStringUni(v.Ptr))); case JsValueType.Managed: return(_context.KeepAliveGet(v.Index)); case JsValueType.JsTypeWrap: //auto unwrap return(this._context.GetObjectProxy(v.Index).WrapObject); case JsValueType.ManagedError: Exception inner = _context.KeepAliveGet(v.Index) as Exception; string msg = null; if (v.Ptr != IntPtr.Zero) { msg = Marshal.PtrToStringUni(v.Ptr); } else { if (inner != null) { msg = inner.Message; } } return(new JsException(msg, inner)); #if NET40 case JsValueType.Wrapped: return(new JsObject(_context, v.Ptr)); #else case JsValueType.Dictionary: return(JsDictionaryObject(v)); #endif case JsValueType.Wrapped: return(new JsObject(_context, v.Ptr)); case JsValueType.Error: return(JsException.Create(this, (JsError)Marshal.PtrToStructure(v.Ptr, typeof(JsError)))); case JsValueType.Function: var fa = new JsValue[2]; for (int i = 0; i < 2; i++) { fa[i] = (JsValue)Marshal.PtrToStructure(new IntPtr(v.Ptr.ToInt64() + (16 * i)), typeof(JsValue)); } return(new JsFunction(_context, fa[0].Ptr, fa[1].Ptr)); default: throw new InvalidOperationException("unknown type code: " + v.Type); } }