Exemplo n.º 1
0
        /// <summary>
        /// Calls the function with the specified arguments expecting a return value of type T.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="args"></param>
        /// <returns></returns>
        public T Call <T>(JsObject thisObj = null, params JsValue[] args)
            where T : JsValue
        {
            var result = CallInternal(thisObj, args);

            return(ValueFactory.CreateValue <T>(result));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Gets the prototype of an object.
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public JsObject GetPrototypeOf(JsObject obj)
        {
            if (obj == null)
            {
                throw new ArgumentNullException(nameof(obj));
            }

            var protoHandle = Engine.JsGetPrototype(obj.Handle);

            return(ValueFactory.CreateValue <JsObject>(protoHandle));
        }
Exemplo n.º 3
0
        /// <summary>
        /// Returns a runtime-independent buffer of the the specified script, which contains unicode characters, that can be reused without requiring the script to be re-parsed.
        /// </summary>
        /// <param name="script"></param>
        /// <param name="isLibraryCode"></param>
        /// <returns></returns>
        public byte[] SerializeUnicodeScript(string unicodeScript, bool isLibraryCode = false)
        {
            if (IsDisposed)
            {
                throw new ObjectDisposedException(nameof(BaristaContext));
            }

            var scriptBuffer            = ValueFactory.CreateArrayBuffer(unicodeScript);
            var parsedArrayBufferHandle = Engine.JsSerialize(scriptBuffer.Handle, JavaScriptParseScriptAttributes.ArrayBufferIsUtf16Encoded);
            var arrayBuffer             = ValueFactory.CreateValue <JsArrayBuffer>(parsedArrayBufferHandle);

            return(arrayBuffer.GetArrayBufferStorage());
        }
Exemplo n.º 4
0
        /// <summary>
        /// Returns a function representing the specified, previously-serialized, script.
        /// </summary>
        /// <param name="serializedScript"></param>
        /// <param name="sourceUrl"></param>
        /// <returns></returns>
        public JsFunction ParseSerializedScript(byte[] serializedScript, Func <string> scriptLoadCallback, string sourceUrl = "[eval code]")
        {
            var buffer      = ValueFactory.CreateArrayBuffer(serializedScript);
            var jsSourceUrl = ValueFactory.CreateString(sourceUrl);

            var callback = new JavaScriptSerializedLoadScriptCallback((JavaScriptSourceContext sourceContext, out IntPtr value, out JavaScriptParseScriptAttributes parseAttributes) =>
            {
                var script      = scriptLoadCallback();
                value           = Engine.JsCreateString(script, (ulong)script.Length).DangerousGetHandle();
                parseAttributes = JavaScriptParseScriptAttributes.None;
                return(true);
            });

            var fnScript = Engine.JsParseSerialized(buffer.Handle, callback, JavaScriptSourceContext.None, jsSourceUrl.Handle);

            return(ValueFactory.CreateValue <JsFunction>(fnScript));
        }
Exemplo n.º 5
0
        /// <summary>
        /// Invokes function as a constructor.
        /// </summary>
        /// <param name="thisObj"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        public JsObject Construct(JsObject thisObj = null, params JsValue[] args)
        {
            //Must at least have a 'thisObject'
            if (thisObj == null)
            {
                thisObj = Context.GlobalObject;
            }

            var argPtrs = args
                          .Select(a => a == null ? Context.Undefined.Handle.DangerousGetHandle() : a.Handle.DangerousGetHandle())
                          .Prepend(thisObj.Handle.DangerousGetHandle())
                          .ToArray();

            var newObjHandle = Engine.JsConstructObject(Handle, argPtrs, (ushort)argPtrs.Length);

            return(ValueFactory.CreateValue <JsObject>(newObjHandle));
        }
Exemplo n.º 6
0
        private Object CreateObject(TerminalToken token)
        {
            switch (token.Symbol.Id)
            {
            case (int)SymbolConstants.SYMBOL_EOF:
                return(null);

            case (int)SymbolConstants.SYMBOL_WHITESPACE:
                return(null);

            case (int)SymbolConstants.SYMBOL_MINUS:
                return(null);

            case (int)SymbolConstants.SYMBOL_LPARAN:
                return(null);

            case (int)SymbolConstants.SYMBOL_RPARAN:
                return(null);

            case (int)SymbolConstants.SYMBOL_TIMES:
                return(null);

            case (int)SymbolConstants.SYMBOL_DIV:
                return(null);

            case (int)SymbolConstants.SYMBOL_PLUS:
                return(null);

            case (int)SymbolConstants.SYMBOL_FLOAT:
                return(ValueFactory.CreateValue(Double.Parse(token.Text,
                                                             numberFormatInfo)));

            case (int)SymbolConstants.SYMBOL_INTEGER:
                return(ValueFactory.CreateValue(Int32.Parse(token.Text)));
            }
            throw new SymbolException("Unknown symbol");
        }
Exemplo n.º 7
0
 public T CreateValue <T>(JavaScriptValueSafeHandle valueHandle) where T : JsValue
 {
     return(ValueFactory.CreateValue <T>(valueHandle));
 }
Exemplo n.º 8
0
 public JsValue CreateValue(Type targetType, JavaScriptValueSafeHandle valueHandle)
 {
     return(ValueFactory.CreateValue(targetType, valueHandle));
 }
Exemplo n.º 9
0
 public JsValue CreateValue(JavaScriptValueSafeHandle valueHandle, JsValueType?valueType = null)
 {
     return(ValueFactory.CreateValue(valueHandle, valueType));
 }
Exemplo n.º 10
0
        /// <summary>
        /// Calls the function with the specified arguments.
        /// </summary>
        /// <param name="args"></param>
        /// <returns></returns>
        public JsValue Call(JsObject thisObj = null, params JsValue[] args)
        {
            var result = CallInternal(thisObj, args);

            return(ValueFactory.CreateValue(result));
        }