The script context

Each script context contains its own global object, distinct from the global object in other script contexts.

Many Chakra hosting APIs require an "active" script context, which can be set using Current. Chakra hosting APIs that require a current context to be set will note that explicitly in their documentation.

コード例 #1
0
		/// <summary>
		/// Initializes a new instance of the <see cref="JsScope"/> struct
		/// </summary>
		/// <param name="context">The context to create the scope for</param>
		public JsScope(JsContext context)
		{
			_disposedFlag = new StatedFlag();
			_previousContext = JsContext.Current;

			JsContext.Current = context;
		}
コード例 #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="JsScope"/> struct
        /// </summary>
        /// <param name="context">The context to create the scope for</param>
        public JsScope(JsContext context)
        {
            _disposed = false;
            _previousContext = JsContext.Current;

            JsContext.Current = context;
        }
コード例 #3
0
		internal static extern JsErrorCode JsCreateContext(JsRuntime runtime, out JsContext newContext);
コード例 #4
0
		internal static extern JsErrorCode JsContextRelease(JsContext reference, out uint count);
コード例 #5
0
		internal static extern JsErrorCode JsContextAddRef(JsContext reference, out uint count);
コード例 #6
0
 internal static extern JsErrorCode JsContextRelease(JsContext reference, out uint count);
コード例 #7
0
		internal static extern JsErrorCode JsGetRuntime(JsContext context, out JsRuntime runtime);
コード例 #8
0
		internal static extern JsErrorCode JsGetContextOfObject(JsValue obj, out JsContext context);
コード例 #9
0
        private EmbeddedType CreateEmbeddedType(Type type)
        {
#if NET40
            Type typeInfo = type;
#else
            TypeInfo typeInfo = type.GetTypeInfo();
#endif
            string            typeName            = type.FullName;
            BindingFlags      defaultBindingFlags = ReflectionHelpers.GetDefaultBindingFlags(true);
            ConstructorInfo[] constructors        = type.GetConstructors(defaultBindingFlags);

            JsNativeFunction nativeConstructorFunction = (callee, isConstructCall, args, argCount, callbackData) =>
            {
                object   result;
                JsValue  resultValue;
                object[] processedArgs = GetHostItemMemberArguments(args);

                if (processedArgs.Length == 0 && typeInfo.IsValueType)
                {
                    result      = Activator.CreateInstance(type);
                    resultValue = MapToScriptType(result);

                    return(resultValue);
                }

                JsValue undefinedValue = JsValue.Undefined;

                if (constructors.Length == 0)
                {
                    CreateAndSetError(string.Format(Strings.Runtime_HostTypeConstructorNotFound, typeName));
                    return(undefinedValue);
                }

                var bestFitConstructor = (ConstructorInfo)ReflectionHelpers.GetBestFitMethod(
                    constructors, processedArgs);
                if (bestFitConstructor == null)
                {
                    CreateAndSetReferenceError(string.Format(
                                                   Strings.Runtime_SuitableConstructorOfHostTypeNotFound, typeName));
                    return(undefinedValue);
                }

                ReflectionHelpers.FixArgumentTypes(ref processedArgs, bestFitConstructor.GetParameters());

                try
                {
                    result = bestFitConstructor.Invoke(processedArgs);
                }
                catch (Exception e)
                {
                    Exception exception        = UnwrapException(e);
                    var       wrapperException = exception as WrapperException;
                    JsValue   errorValue       = wrapperException != null?
                                                 CreateErrorFromWrapperException(wrapperException)
                                                     :
                                                     JsErrorHelpers.CreateError(string.Format(
                                                                                    Strings.Runtime_HostTypeConstructorInvocationFailed, typeName, exception.Message))
                    ;

                    JsContext.SetException(errorValue);

                    return(undefinedValue);
                }

                resultValue = MapToScriptType(result);

                return(resultValue);
            };

            GCHandle embeddedTypeHandle = GCHandle.Alloc(type);
            IntPtr   embeddedTypePtr    = GCHandle.ToIntPtr(embeddedTypeHandle);
            JsValue  objValue           = JsValue.CreateExternalObject(embeddedTypePtr, _embeddedTypeFinalizeCallback);

            JsValue typeValue = JsValue.CreateFunction(nativeConstructorFunction);
            SetNonEnumerableProperty(typeValue, ExternalObjectPropertyName, objValue);

            var embeddedType = new EmbeddedType(type, typeValue,
                                                new List <JsNativeFunction> {
                nativeConstructorFunction
            });

            ProjectFields(embeddedType);
            ProjectProperties(embeddedType);
            ProjectMethods(embeddedType);
            FreezeObject(typeValue);

            return(embeddedType);
        }
コード例 #10
0
 internal static extern JsErrorCode JsGetRuntime(JsContext context, out JsRuntime runtime);
コード例 #11
0
 internal static extern JsErrorCode JsSetContextData(JsContext context, IntPtr data);
コード例 #12
0
 internal static extern JsErrorCode JsGetContextOfObject(JsValue obj, out JsContext context);
コード例 #13
0
 internal static extern JsErrorCode JsSetCurrentContext(JsContext context);
コード例 #14
0
 internal static extern JsErrorCode JsGetCurrentContext(out JsContext currentContext);
コード例 #15
0
 internal static extern JsErrorCode JsCreateContext(JsRuntime runtime, out JsContext newContext);
コード例 #16
0
		internal static extern JsErrorCode JsGetCurrentContext(out JsContext currentContext);
コード例 #17
0
		internal static extern JsErrorCode JsSetCurrentContext(JsContext context);
コード例 #18
0
        private void ProjectProperties(EmbeddedItem externalItem)
        {
            Type    type      = externalItem.HostType;
            object  obj       = externalItem.HostObject;
            JsValue typeValue = externalItem.ScriptValue;
            IList <JsNativeFunction> nativeFunctions = externalItem.NativeFunctions;
            bool instance = externalItem.IsInstance;

            string       typeName            = type.FullName;
            BindingFlags defaultBindingFlags = ReflectionHelpers.GetDefaultBindingFlags(instance);

            PropertyInfo[] properties = type.GetProperties(defaultBindingFlags);

            foreach (PropertyInfo property in properties)
            {
                string propertyName = property.Name;

                JsValue descriptorValue = JsValue.CreateObject();
                descriptorValue.SetProperty("enumerable", JsValue.True, true);

                if (property.GetGetMethod() != null)
                {
                    JsNativeFunction nativeGetFunction = (callee, isConstructCall, args, argCount, callbackData) =>
                    {
                        JsValue undefinedValue = JsValue.Undefined;

                        if (instance && obj == null)
                        {
                            CreateAndSetTypeError(string.Format(
                                                      Strings.Runtime_InvalidThisContextForHostObjectProperty, propertyName));
                            return(undefinedValue);
                        }

                        object result;

                        try
                        {
                            result = property.GetValue(obj, new object[0]);
                        }
                        catch (Exception e)
                        {
                            Exception exception        = UnwrapException(e);
                            var       wrapperException = exception as WrapperException;
                            JsValue   errorValue;

                            if (wrapperException != null)
                            {
                                errorValue = CreateErrorFromWrapperException(wrapperException);
                            }
                            else
                            {
                                string errorMessage = instance ?
                                                      string.Format(Strings.Runtime_HostObjectPropertyGettingFailed, propertyName,
                                                                    exception.Message)
                                                                        :
                                                      string.Format(Strings.Runtime_HostTypePropertyGettingFailed, propertyName,
                                                                    typeName, exception.Message)
                                ;
                                errorValue = JsErrorHelpers.CreateError(errorMessage);
                            }
                            JsContext.SetException(errorValue);

                            return(undefinedValue);
                        }

                        JsValue resultValue = MapToScriptType(result);

                        return(resultValue);
                    };
                    nativeFunctions.Add(nativeGetFunction);

                    JsValue getMethodValue = JsValue.CreateFunction(nativeGetFunction);
                    descriptorValue.SetProperty("get", getMethodValue, true);
                }

                if (property.GetSetMethod() != null)
                {
                    JsNativeFunction nativeSetFunction = (callee, isConstructCall, args, argCount, callbackData) =>
                    {
                        JsValue undefinedValue = JsValue.Undefined;

                        if (instance && obj == null)
                        {
                            CreateAndSetTypeError(string.Format(
                                                      Strings.Runtime_InvalidThisContextForHostObjectProperty, propertyName));
                            return(undefinedValue);
                        }

                        object value = MapToHostType(args[1]);
                        ReflectionHelpers.FixPropertyValueType(ref value, property);

                        try
                        {
                            property.SetValue(obj, value, new object[0]);
                        }
                        catch (Exception e)
                        {
                            Exception exception        = UnwrapException(e);
                            var       wrapperException = exception as WrapperException;
                            JsValue   errorValue;

                            if (wrapperException != null)
                            {
                                errorValue = CreateErrorFromWrapperException(wrapperException);
                            }
                            else
                            {
                                string errorMessage = instance ?
                                                      string.Format(Strings.Runtime_HostObjectPropertySettingFailed, propertyName,
                                                                    exception.Message)
                                                                        :
                                                      string.Format(Strings.Runtime_HostTypePropertySettingFailed, propertyName,
                                                                    typeName, exception.Message)
                                ;
                                errorValue = JsErrorHelpers.CreateError(errorMessage);
                            }
                            JsContext.SetException(errorValue);

                            return(undefinedValue);
                        }

                        return(undefinedValue);
                    };
                    nativeFunctions.Add(nativeSetFunction);

                    JsValue setMethodValue = JsValue.CreateFunction(nativeSetFunction);
                    descriptorValue.SetProperty("set", setMethodValue, true);
                }

                typeValue.DefineProperty(propertyName, descriptorValue);
            }
        }
コード例 #19
0
		internal static extern JsErrorCode JsSetContextData(JsContext context, IntPtr data);
コード例 #20
0
        private void ProjectMethods(EmbeddedItem externalItem)
        {
            Type    type      = externalItem.HostType;
            object  obj       = externalItem.HostObject;
            JsValue typeValue = externalItem.ScriptValue;
            IList <JsNativeFunction> nativeFunctions = externalItem.NativeFunctions;
            bool instance = externalItem.IsInstance;

            string                   typeName            = type.FullName;
            BindingFlags             defaultBindingFlags = ReflectionHelpers.GetDefaultBindingFlags(instance);
            IEnumerable <MethodInfo> methods             = type.GetMethods(defaultBindingFlags)
                                                           .Where(ReflectionHelpers.IsFullyFledgedMethod);
            IEnumerable <IGrouping <string, MethodInfo> > methodGroups = methods.GroupBy(m => m.Name);

            foreach (IGrouping <string, MethodInfo> methodGroup in methodGroups)
            {
                string       methodName       = methodGroup.Key;
                MethodInfo[] methodCandidates = methodGroup.ToArray();

                JsNativeFunction nativeFunction = (callee, isConstructCall, args, argCount, callbackData) =>
                {
                    JsValue undefinedValue = JsValue.Undefined;

                    if (instance && obj == null)
                    {
                        CreateAndSetTypeError(string.Format(
                                                  Strings.Runtime_InvalidThisContextForHostObjectMethod, methodName));
                        return(undefinedValue);
                    }

                    object[] processedArgs = GetHostItemMemberArguments(args);

                    var bestFitMethod = (MethodInfo)ReflectionHelpers.GetBestFitMethod(
                        methodCandidates, processedArgs);
                    if (bestFitMethod == null)
                    {
                        CreateAndSetReferenceError(string.Format(
                                                       Strings.Runtime_SuitableMethodOfHostObjectNotFound, methodName));
                        return(undefinedValue);
                    }

                    ReflectionHelpers.FixArgumentTypes(ref processedArgs, bestFitMethod.GetParameters());

                    object result;

                    try
                    {
                        result = bestFitMethod.Invoke(obj, processedArgs);
                    }
                    catch (Exception e)
                    {
                        Exception exception        = UnwrapException(e);
                        var       wrapperException = exception as WrapperException;
                        JsValue   errorValue;

                        if (wrapperException != null)
                        {
                            errorValue = CreateErrorFromWrapperException(wrapperException);
                        }
                        else
                        {
                            string errorMessage = instance ?
                                                  string.Format(Strings.Runtime_HostObjectMethodInvocationFailed, methodName,
                                                                exception.Message)
                                                                :
                                                  string.Format(Strings.Runtime_HostTypeMethodInvocationFailed, methodName, typeName,
                                                                exception.Message)
                            ;
                            errorValue = JsErrorHelpers.CreateError(errorMessage);
                        }
                        JsContext.SetException(errorValue);

                        return(undefinedValue);
                    }

                    JsValue resultValue = MapToScriptType(result);

                    return(resultValue);
                };
                nativeFunctions.Add(nativeFunction);

                JsValue methodValue = JsValue.CreateFunction(nativeFunction);
                typeValue.SetProperty(methodName, methodValue, true);
            }
        }
コード例 #21
0
        private static void CreateAndSetTypeError(string message)
        {
            JsValue errorValue = JsErrorHelpers.CreateTypeError(message);

            JsContext.SetException(errorValue);
        }
コード例 #22
0
 internal static extern JsErrorCode JsContextAddRef(JsContext reference, out uint count);