Ejemplo n.º 1
0
        /// <summary>
        /// fill array to native js value
        /// </summary>
        /// <param name="arr"></param>
        /// <param name="output"></param>
        public void ToJsValue(object[] arr, ref JsValue output)
        {
            int len = arr.Length;

            //alloc array
            JsContext.jsvalue_alloc_array(len, ref output);
            if (output.I32 != len)
            {
                throw new JsInteropException("can't allocate memory on the unmanaged side");
            }
            unsafe
            {
                JsValue **nativeArr = (JsValue **)output.Ptr;
                for (int i = 0; i < len; i++)
                {
                    AnyToJsValuePtr(arr[i], nativeArr[i]);
                }
            }
        }
Ejemplo n.º 2
0
        JsObject CreateJsDictionaryObject(ref JsValue v)
        {
            //js dic is key-pair object
            JsObject obj   = new JsObject(this._context, v.Ptr);
            int      count = v.I32 * 2;//key and value

            unsafe
            {
                JsValue **arr = (JsValue **)v.Ptr;
                for (int i = 0; i < count;)
                {
                    JsValue *key   = arr[i];
                    JsValue *value = arr[i + 1];

                    //TODO: review when key is not string
                    obj[(string)FromJsValuePtr(key)] = FromJsValuePtr(value);
                    i += 2;
                }
            }
            return(obj);
        }
Ejemplo n.º 3
0
 public static extern OneByteBoolean JsValueIsEqual([NativeTypeName("JSContextRef")] JsContext *ctx, [NativeTypeName("JSValueRef")] JsValue *a, [NativeTypeName("JSValueRef")] JsValue *b,
                                                    [NativeTypeName("JSValueRef *")] JsValue **exception);
Ejemplo n.º 4
0
 public static extern UIntPtr JsObjectGetArrayBufferByteLength([NativeTypeName("JSContextRef")] JsContext *ctx, [NativeTypeName("JSObjectRef")] JsValue * @object, [NativeTypeName("JSValueRef *")] JsValue **exception);
Ejemplo n.º 5
0
 public static extern JsValue *JsObjectGetTypedArrayBuffer([NativeTypeName("JSContextRef")] JsContext *ctx, [NativeTypeName("JSObjectRef")] JsValue * @object, [NativeTypeName("JSValueRef *")] JsValue **exception);
Ejemplo n.º 6
0
 public static extern JsValue *JsObjectMakeTypedArrayWithArrayBufferAndOffset([NativeTypeName("JSContextRef")] JsContext *ctx, JsTypedArrayType arrayType, [NativeTypeName("JSObjectRef")] JsValue *buffer,
                                                                              [NativeTypeName("size_t")] UIntPtr byteOffset, [NativeTypeName("size_t")] UIntPtr length, [NativeTypeName("JSValueRef *")] JsValue **exception);
Ejemplo n.º 7
0
 public static JsValue *JsObjectMakeTypedArrayWithBytesNoCopy(JsContext *ctx, JsTypedArrayType arrayType, void *bytes, UIntPtr byteLength, FnPtr <TypedArrayBytesDeallocatorCallback> bytesDeallocator, void *deallocatorContext,
                                                              JsValue **exception)
 => JsObjectMakeTypedArrayWithBytesNoCopy(ctx, arrayType, bytes, byteLength, (IntPtr)bytesDeallocator, deallocatorContext, exception);
Ejemplo n.º 8
0
 public static extern JsValue *JsObjectCallAsConstructor([NativeTypeName("JSContextRef")] JsContext *ctx, [NativeTypeName("JSObjectRef")] JsValue * @object, [NativeTypeName("size_t")] UIntPtr argumentCount,
                                                         [NativeTypeName("const JSValueRef []")]
                                                         JsValue **arguments, [NativeTypeName("JSValueRef *")] JsValue **exception);
Ejemplo n.º 9
0
 public static extern JsString *JsValueToStringCopy([NativeTypeName("JSContextRef")] JsContext *ctx, [NativeTypeName("JSValueRef")] JsValue *value, [NativeTypeName("JSValueRef *")] JsValue **exception);
Ejemplo n.º 10
0
 public static extern double JsValueToNumber([NativeTypeName("JSContextRef")] JsContext *ctx, [NativeTypeName("JSValueRef")] JsValue *value, [NativeTypeName("JSValueRef *")] JsValue **exception);
Ejemplo n.º 11
0
 public static extern JsString *JsValueCreateJsonString([NativeTypeName("JSContextRef")] JsContext *ctx, [NativeTypeName("JSValueRef")] JsValue *value, [NativeTypeName("unsigned int")] uint indent,
                                                        [NativeTypeName("JSValueRef *")] JsValue **exception);
Ejemplo n.º 12
0
 public static extern JsValue *EvaluateScript([NativeTypeName("JSContextRef")] JsContext *ctx, [NativeTypeName("JSStringRef")] JsString *script, [NativeTypeName("JSObjectRef")] JsValue *thisObject,
                                              [NativeTypeName("JSStringRef")] JsString *sourceUrl, int startingLineNumber, [NativeTypeName("JSValueRef *")] JsValue **exception);
Ejemplo n.º 13
0
        public unsafe void AnyToJsValuePtr(object obj, JsValue *output)
        {
            if (obj == null)
            {
                output->Type = JsValueType.Null;
                return;
            }
            //-----

            if (obj is INativeRef)
            {
                //extension
                INativeRef prox = (INativeRef)obj;
                output->I32  = _context.KeepAliveAdd(obj);
                output->Type = JsValueType.JsTypeWrap;
                output->Ptr  = prox.UnmanagedPtr;
                return;
            }
            //-----
            Type type = obj.GetType();

            // Check for nullable types (we will cast the value out of the box later).
            type = type.ExtGetInnerTypeIfNullableValue();
            if (type == typeof(Boolean))
            {
                output->Type = JsValueType.Boolean;
                output->I32  = (bool)obj ? 1 : 0;
                return;
            }

            if (type == typeof(String) || type == typeof(Char))
            {
                // We need to allocate some memory on the other side;
                // will be free'd by unmanaged code.
                JsContext.jsvalue_alloc_string(obj.ToString(), output);
                output->Type = JsValueType.String;
                return;
            }
            //-----------------------------------------------------------
            if (type == typeof(Byte))
            {
                output->Type = JsValueType.Integer;
                output->I32  = (int)(byte)obj;
                return;
            }
            if (type == typeof(Int16))
            {
                output->Type = JsValueType.Integer;
                output->I32  = (int)(Int16)obj;
                return;
            }
            if (type == typeof(UInt16))
            {
                output->Type = JsValueType.Integer;
                output->I32  = (int)(UInt16)obj;
                return;
            }
            if (type == typeof(Int32))
            {
                output->Type = JsValueType.Integer;
                output->I32  = (int)obj;
                return;
            }

            if (type == typeof(UInt32))
            {
                //TODO: review Type here when send to native side
                output->Type = JsValueType.Integer;
                output->I32  = (int)(uint)obj;
                return;
            }

            if (type == typeof(Int64))
            {
                output->Type = JsValueType.Number;
                output->Num  = (double)(Int64)obj;
                return;
            }

            if (type == typeof(UInt64))
            {
                output->Type = JsValueType.Number;
                output->Num  = (double)(UInt64)obj;
                return;
            }


            if (type == typeof(Single))
            {
                output->Type = JsValueType.Number;
                output->Num  = (double)(Single)obj;
                return;
            }


            if (type == typeof(Double))
            {
                output->Type = JsValueType.Number;
                output->Num  = (double)obj;
                return;
            }

            if (type == typeof(Decimal))
            {
                //TODO: review here
                //.net decimal is larger than double?
                output->Type = JsValueType.Number;
                output->Num  = (double)(Decimal)obj;
                return;
            }

            if (type == typeof(DateTime))
            {
                output->Type = JsValueType.Date;
                output->Num  = Convert.ToInt64(((DateTime)obj).Subtract(EPOCH).TotalMilliseconds); /*(((DateTime)obj).Ticks - 621355968000000000.0 + 26748000000000.0)/10000.0*/
                return;
            }
            // Arrays of anything that can be cast to object[] are recursively convertef after
            // allocating an appropriate jsvalue on the unmanaged side.

            var array = obj as object[];

            if (array != null)
            {
                //alloc space for array
                int arrLen = array.Length;
                JsContext.jsvalue_alloc_array(arrLen, output);

                if (output->I32 != arrLen)
                {
                    throw new JsInteropException("can't allocate memory on the unmanaged side");
                }
                //

                output->Type = JsValueType.Array;
                unsafe
                {
                    JsValue **arr = (JsValue **)output->Ptr;
                    for (int i = 0; i < arrLen; i++)
                    {
                        AnyToJsValuePtr(array[i], arr[i]);
                    }
                }

                return;
            }

            // Every object explicitly converted to a value becomes an entry of the
            // _keepalives list, to make sure the GC won't collect it while still in
            // use by the unmanaged Javascript engine. We don't try to track duplicates
            // because adding the same object more than one time acts more or less as
            // reference counting.
            //check

            JsTypeDefinition jsTypeDefinition = _context.GetJsTypeDefinition(type);
            INativeRef       prox2            = _context.CreateWrapper(obj, jsTypeDefinition);

            //
            output->Type = JsValueType.JsTypeWrap;
            output->Ptr  = prox2.UnmanagedPtr;
            output->I32  = prox2.ManagedIndex;
        }
Ejemplo n.º 14
0
        /// <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(this._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[0]->Ptr, arr[1]->Ptr));
                }

            default:
                throw new InvalidOperationException("unknown type code: " + v.Type);
            }
        }
Ejemplo n.º 15
0
 public static extern OneByteBoolean JsObjectDeletePropertyForKey([NativeTypeName("JSContextRef")] JsContext *ctx, [NativeTypeName("JSObjectRef")] JsValue * @object, [NativeTypeName("JSValueRef")] JsValue *propertyKey,
                                                                  [NativeTypeName("JSValueRef *")] JsValue **exception);
Ejemplo n.º 16
0
 public static extern void JsObjectSetPropertyAtIndex([NativeTypeName("JSContextRef")] JsContext *ctx, [NativeTypeName("JSObjectRef")] JsValue * @object, [NativeTypeName("unsigned int")] uint propertyIndex,
                                                      [NativeTypeName("JSValueRef")] JsValue *value, [NativeTypeName("JSValueRef *")] JsValue **exception);
Ejemplo n.º 17
0
 public static extern JsValue *JsValueToObject([NativeTypeName("JSContextRef")] JsContext *ctx, [NativeTypeName("JSValueRef")] JsValue *value, [NativeTypeName("JSValueRef *")] JsValue **exception);
Ejemplo n.º 18
0
 public static extern JsValue *JsObjectMakeTypedArray([NativeTypeName("JSContextRef")] JsContext *ctx, JsTypedArrayType arrayType, [NativeTypeName("size_t")] UIntPtr length, [NativeTypeName("JSValueRef *")] JsValue **exception);
Ejemplo n.º 19
0
 public static extern OneByteBoolean CheckScriptSyntax([NativeTypeName("JSContextRef")] JsContext *ctx, [NativeTypeName("JSStringRef")] JsString *script, [NativeTypeName("JSStringRef")] JsString *sourceUrl, int startingLineNumber,
                                                       [NativeTypeName("JSValueRef *")] JsValue **exception);
Ejemplo n.º 20
0
 public static extern JsValue *JsObjectMakeTypedArrayWithArrayBuffer([NativeTypeName("JSContextRef")] JsContext *ctx, JsTypedArrayType arrayType, [NativeTypeName("JSObjectRef")] JsValue *buffer,
                                                                     [NativeTypeName("JSValueRef *")] JsValue **exception);
Ejemplo n.º 21
0
 public static extern JsValue *JsObjectMakeDeferredPromise([NativeTypeName("JSContextRef")] JsContext *ctx, [NativeTypeName("JSObjectRef *")] JsValue **resolve, [NativeTypeName("JSObjectRef *")] JsValue **reject,
                                                           [NativeTypeName("JSValueRef *")] JsValue **exception);
Ejemplo n.º 22
0
 public static extern UIntPtr JsObjectGetTypedArrayByteOffset([NativeTypeName("JSContextRef")] JsContext *ctx, [NativeTypeName("JSObjectRef")] JsValue * @object, [NativeTypeName("JSValueRef *")] JsValue **exception);
Ejemplo n.º 23
0
 public static extern JsValue *JsObjectMakeFunction([NativeTypeName("JSContextRef")] JsContext *ctx, [NativeTypeName("JSStringRef")] JsString *name, [NativeTypeName("unsigned int")] uint parameterCount,
                                                    [NativeTypeName("const JSStringRef []")]
                                                    JsString **parameterNames, [NativeTypeName("JSStringRef")] JsString *body, [NativeTypeName("JSStringRef")] JsString *sourceUrl, int startingLineNumber, [NativeTypeName("JSValueRef *")] JsValue **exception);
Ejemplo n.º 24
0
 public static extern JsValue *JsObjectMakeArrayBufferWithBytesNoCopy([NativeTypeName("JSContextRef")] JsContext *ctx, [NativeTypeName("void *")] void *bytes, [NativeTypeName("size_t")] UIntPtr byteLength,
                                                                      [NativeTypeName("JSTypedArrayBytesDeallocator")]
                                                                      IntPtr bytesDeallocator, [NativeTypeName("void *")] void *deallocatorContext, [NativeTypeName("JSValueRef *")] JsValue **exception);
Ejemplo n.º 25
0
 public static extern JsValue *JsObjectGetProperty([NativeTypeName("JSContextRef")] JsContext *ctx, [NativeTypeName("JSObjectRef")] JsValue * @object, [NativeTypeName("JSStringRef")] JsString *propertyName,
                                                   [NativeTypeName("JSValueRef *")] JsValue **exception);
Ejemplo n.º 26
0
 public static extern JsTypedArrayType JsValueGetTypedArrayType([NativeTypeName("JSContextRef")] JsContext *ctx, [NativeTypeName("JSValueRef")] JsValue *value, [NativeTypeName("JSValueRef *")] JsValue **exception);
Ejemplo n.º 27
0
 public static extern JsValue *JsObjectGetPropertyForKey([NativeTypeName("JSContextRef")] JsContext *ctx, [NativeTypeName("JSObjectRef")] JsValue * @object, [NativeTypeName("JSValueRef")] JsValue *propertyKey,
                                                         [NativeTypeName("JSValueRef *")] JsValue **exception);
Ejemplo n.º 28
0
 public static extern OneByteBoolean JsValueIsInstanceOfConstructor([NativeTypeName("JSContextRef")] JsContext *ctx, [NativeTypeName("JSValueRef")] JsValue *value, [NativeTypeName("JSObjectRef")] JsValue *constructor,
                                                                    [NativeTypeName("JSValueRef *")] JsValue **exception);
Ejemplo n.º 29
0
 public static extern void JsObjectSetPropertyForKey([NativeTypeName("JSContextRef")] JsContext *ctx, [NativeTypeName("JSObjectRef")] JsValue * @object, [NativeTypeName("JSValueRef")] JsValue *propertyKey,
                                                     [NativeTypeName("JSValueRef")] JsValue *value, [NativeTypeName("JSPropertyAttributes")]
                                                     JsPropertyAttribute attributes, [NativeTypeName("JSValueRef *")] JsValue **exception);