private JsValue CreateFunctionFromDelegate(Delegate value) { JsNativeFunction nativeFunction = (callee, isConstructCall, args, argCount, callbackData) => { object[] processedArgs = MapToHostType(args.Skip(1).ToArray()); ParameterInfo[] parameters = value.GetMethodInfo().GetParameters(); JsValue undefinedValue = JsValue.Undefined; ReflectionHelpers.FixArgumentTypes(ref processedArgs, parameters); object result; try { result = value.DynamicInvoke(processedArgs); } catch (Exception e) { JsValue errorValue = JsErrorHelpers.CreateError( string.Format(Strings.Runtime_HostDelegateInvocationFailed, e.Message)); JsErrorHelpers.SetException(errorValue); return(undefinedValue); } JsValue resultValue = MapToScriptType(result); return(resultValue); }; _nativeFunctions.Add(nativeFunction); JsValue functionValue = JsValue.CreateFunction(nativeFunction); return(functionValue); }
public static Task DeclareFunctionAsync(this JsContext context, string name, Func <object> func) { return(context.RequestScopeAsync(session => session.Global.SetProperty(name, JsValue.CreateFunction(() => JsValue.FromObject(func()))))); }
public static Task DeclareFunctionAsync(this JsContext context, string name, Action action) { return(context.RequestScopeAsync(session => session.Global.SetProperty(name, JsValue.CreateFunction(action)))); }
private void ProjectProperties(EmbeddedItem externalItem, EmbeddingObjectOptions options) { 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).Where(options.IsMapped).ToArray(); foreach (PropertyInfo property in properties) { string propertyName = property.Name; JsValue descriptorValue = JsValue.CreateObject(); descriptorValue.SetProperty("enumerable", JsValue.True, true); if (property.GetGetMethod() != null) { JsValue nativeGetFunction(JsValue callee, bool isConstructCall, JsValue[] args, ushort argCount, IntPtr callbackData) { if (instance && obj == null) { CreateAndSetError($"Invalid context for '{propertyName}' property."); return(JsValue.Undefined); } object result; try { result = property.GetValue(obj, new object[0]); } catch (Exception e) { Exception exception = UnwrapException(e); var wrapperException = exception as JsException; JsValue errorValue; if (wrapperException != null) { errorValue = CreateErrorFromWrapperException(wrapperException); } else { string errorMessage = instance ? $"Property '{propertyName}' get operation failed: {exception.Message}" : $"Property '{propertyName}' of static type '{typeName}' get operation failed: {exception.Message}" ; errorValue = JsValue.CreateError(JsValue.FromString(errorMessage)); } JsContext.SetException(errorValue); return(JsValue.Undefined); } JsValue resultValue = MapToScriptType(result); return(resultValue); } nativeFunctions.Add(nativeGetFunction); JsValue getMethodValue = JsValue.CreateFunction(nativeGetFunction); descriptorValue.SetProperty("get", getMethodValue, true); } if (property.GetSetMethod() != null) { JsValue nativeSetFunction(JsValue callee, bool isConstructCall, JsValue[] args, ushort argCount, IntPtr callbackData) { JsValue undefinedValue = JsValue.Undefined; if (instance && obj == null) { CreateAndSetError($"Invalid context for '{propertyName}' property."); 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 JsException; JsValue errorValue; if (wrapperException != null) { errorValue = CreateErrorFromWrapperException(wrapperException); } else { string errorMessage = instance ? $"Host object property '{propertyName}' setting failed: {exception.Message}" : $"Host type '{typeName}' property '{propertyName}' setting failed: {exception.Message}" ; errorValue = JsValue.CreateError(JsValue.FromString(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); } }
private void ProjectFields(EmbeddedItem externalItem, EmbeddingObjectOptions options) { Type type = externalItem.HostType; object obj = externalItem.HostObject; JsValue typeValue = externalItem.ScriptValue; bool instance = externalItem.IsInstance; IList <JsNativeFunction> nativeFunctions = externalItem.NativeFunctions; string typeName = type.FullName; BindingFlags defaultBindingFlags = ReflectionHelpers.GetDefaultBindingFlags(instance); FieldInfo[] fields = type.GetFields(defaultBindingFlags).Where(options.IsMapped).ToArray(); foreach (FieldInfo field in fields) { string fieldName = field.Name; JsValue descriptorValue = JsValue.CreateObject(); descriptorValue.SetProperty("enumerable", JsValue.True, true); JsNativeFunction nativeGetFunction = (callee, isConstructCall, args, argCount, callbackData) => { if (instance && obj == null) { CreateAndSetError($"Context error while invoking getter '{fieldName}'."); return(JsValue.Undefined); } object result; try { result = field.GetValue(obj); } catch (Exception e) { Exception exception = UnwrapException(e); var wrapperException = exception as JsException; JsValue errorValue; if (wrapperException != null) { errorValue = CreateErrorFromWrapperException(wrapperException); } else { string errorMessage = instance ? $"Error ocured while reading field '{fieldName}': {exception.Message}" : $"Erorr ocured while reading static field '{fieldName}' from type '{typeName}': {exception.Message}" ; errorValue = JsValue.CreateError(JsValue.FromString(errorMessage)); } JsContext.SetException(errorValue); return(JsValue.Undefined); } JsValue resultValue = MapToScriptType(result); return(resultValue); }; nativeFunctions.Add(nativeGetFunction); JsValue getMethodValue = JsValue.CreateFunction(nativeGetFunction); descriptorValue.SetProperty("get", getMethodValue, true); JsNativeFunction nativeSetFunction = (callee, isConstructCall, args, argCount, callbackData) => { if (instance && obj == null) { CreateAndSetError($"Invalid context got host object field {fieldName}."); return(JsValue.Undefined); } object value = MapToHostType(args[1]); ReflectionHelpers.FixFieldValueType(ref value, field); try { field.SetValue(obj, value); } catch (Exception e) { Exception exception = UnwrapException(e); var wrapperException = exception as JsException; JsValue errorValue; if (wrapperException != null) { errorValue = CreateErrorFromWrapperException(wrapperException); } else { string errorMessage = instance ? $"Failed to set value for hosts object field '{fieldName}': {exception.Message}" : $"Failed to set value for static type '{typeName}' field '{fieldName}': {exception.Message}" ; errorValue = JsValue.CreateError(JsValue.FromString(errorMessage)); } JsContext.SetException(errorValue); return(JsValue.Undefined); } return(JsValue.Undefined); }; nativeFunctions.Add(nativeSetFunction); JsValue setMethodValue = JsValue.CreateFunction(nativeSetFunction); descriptorValue.SetProperty("set", setMethodValue, true); typeValue.DefineProperty(fieldName, descriptorValue); } }
private void ProjectMethods(JsValue target, Type type, bool instance) { string typeName = type.FullName; BindingFlags defaultBindingFlags = ReflectionHelpers.GetDefaultBindingFlags(instance); MethodInfo[] methods = type.GetMethods(defaultBindingFlags); 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 thisValue = args[0]; JsValue undefinedValue = JsValue.Undefined; object thisObj = null; if (instance) { if (!thisValue.HasExternalData) { JsValue errorValue = JsErrorHelpers.CreateTypeError( string.Format(Strings.Runtime_InvalidThisContextForHostObjectMethod, methodName)); JsErrorHelpers.SetException(errorValue); return(undefinedValue); } thisObj = MapToHostType(thisValue); } object[] processedArgs = MapToHostType(args.Skip(1).ToArray()); var bestFitMethod = (MethodInfo)ReflectionHelpers.GetBestFitMethod( methodCandidates, processedArgs); if (bestFitMethod == null) { JsValue errorValue = JsErrorHelpers.CreateReferenceError( string.Format(Strings.Runtime_SuitableMethodOfHostObjectNotFound, methodName)); JsErrorHelpers.SetException(errorValue); return(undefinedValue); } ReflectionHelpers.FixArgumentTypes(ref processedArgs, bestFitMethod.GetParameters()); object result; try { result = bestFitMethod.Invoke(thisObj, processedArgs); } catch (Exception e) { string errorMessage = instance ? string.Format( Strings.Runtime_HostObjectMethodInvocationFailed, methodName, e.Message) : string.Format( Strings.Runtime_HostTypeMethodInvocationFailed, methodName, typeName, e.Message) ; JsValue errorValue = JsErrorHelpers.CreateError(errorMessage); JsErrorHelpers.SetException(errorValue); return(undefinedValue); } JsValue resultValue = MapToScriptType(result); return(resultValue); }; _nativeFunctions.Add(nativeFunction); JsValue methodValue = JsValue.CreateFunction(nativeFunction); target.SetProperty(methodName, methodValue, true); } }
private void ProjectProperties(JsValue target, Type type, bool instance) { 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 nativeFunction = (callee, isConstructCall, args, argCount, callbackData) => { JsValue thisValue = args[0]; JsValue undefinedValue = JsValue.Undefined; object thisObj = null; if (instance) { if (!thisValue.HasExternalData) { JsValue errorValue = JsErrorHelpers.CreateTypeError( string.Format(Strings.Runtime_InvalidThisContextForHostObjectProperty, propertyName)); JsErrorHelpers.SetException(errorValue); return(undefinedValue); } thisObj = MapToHostType(thisValue); } object result; try { result = property.GetValue(thisObj, new object[0]); } catch (Exception e) { string errorMessage = instance ? string.Format( Strings.Runtime_HostObjectPropertyGettingFailed, propertyName, e.Message) : string.Format( Strings.Runtime_HostTypePropertyGettingFailed, propertyName, typeName, e.Message) ; JsValue errorValue = JsErrorHelpers.CreateError(errorMessage); JsErrorHelpers.SetException(errorValue); return(undefinedValue); } JsValue resultValue = MapToScriptType(result); return(resultValue); }; _nativeFunctions.Add(nativeFunction); JsValue getMethodValue = JsValue.CreateFunction(nativeFunction); descriptorValue.SetProperty("get", getMethodValue, true); } if (property.GetSetMethod() != null) { JsNativeFunction nativeFunction = (callee, isConstructCall, args, argCount, callbackData) => { JsValue thisValue = args[0]; JsValue undefinedValue = JsValue.Undefined; object thisObj = null; if (instance) { if (!thisValue.HasExternalData) { JsValue errorValue = JsErrorHelpers.CreateTypeError( string.Format(Strings.Runtime_InvalidThisContextForHostObjectProperty, propertyName)); JsErrorHelpers.SetException(errorValue); return(undefinedValue); } thisObj = MapToHostType(thisValue); } object value = MapToHostType(args.Skip(1).First()); ReflectionHelpers.FixPropertyValueType(ref value, property); try { property.SetValue(thisObj, value, new object[0]); } catch (Exception e) { string errorMessage = instance ? string.Format( Strings.Runtime_HostObjectPropertySettingFailed, propertyName, e.Message) : string.Format( Strings.Runtime_HostTypePropertySettingFailed, propertyName, typeName, e.Message) ; JsValue errorValue = JsErrorHelpers.CreateError(errorMessage); JsErrorHelpers.SetException(errorValue); return(undefinedValue); } return(undefinedValue); }; _nativeFunctions.Add(nativeFunction); JsValue setMethodValue = JsValue.CreateFunction(nativeFunction); descriptorValue.SetProperty("set", setMethodValue, true); } target.DefineProperty(propertyName, descriptorValue); } }
private JsValue CreateConstructor(Type type) { TypeInfo typeInfo = type.GetTypeInfo(); string typeName = type.FullName; BindingFlags defaultBindingFlags = ReflectionHelpers.GetDefaultBindingFlags(true); ConstructorInfo[] constructors = type.GetConstructors(defaultBindingFlags); JsNativeFunction nativeFunction = (callee, isConstructCall, args, argCount, callbackData) => { JsValue resultValue; JsValue undefinedValue = JsValue.Undefined; object[] processedArgs = MapToHostType(args.Skip(1).ToArray()); object result; if (processedArgs.Length == 0 && typeInfo.IsValueType) { result = Activator.CreateInstance(type); resultValue = MapToScriptType(result); return(resultValue); } if (constructors.Length == 0) { JsValue errorValue = JsErrorHelpers.CreateError( string.Format(Strings.Runtime_HostTypeConstructorNotFound, typeName)); JsErrorHelpers.SetException(errorValue); return(undefinedValue); } var bestFitConstructor = (ConstructorInfo)ReflectionHelpers.GetBestFitMethod( constructors, processedArgs); if (bestFitConstructor == null) { JsValue errorValue = JsErrorHelpers.CreateReferenceError( string.Format(Strings.Runtime_SuitableConstructorOfHostTypeNotFound, typeName)); JsErrorHelpers.SetException(errorValue); return(undefinedValue); } ReflectionHelpers.FixArgumentTypes(ref processedArgs, bestFitConstructor.GetParameters()); try { result = bestFitConstructor.Invoke(processedArgs); } catch (Exception e) { JsValue errorValue = JsErrorHelpers.CreateError( string.Format(Strings.Runtime_HostTypeConstructorInvocationFailed, typeName, e.Message)); JsErrorHelpers.SetException(errorValue); return(undefinedValue); } resultValue = MapToScriptType(result); return(resultValue); }; _nativeFunctions.Add(nativeFunction); JsValue constructorValue = JsValue.CreateFunction(nativeFunction); return(constructorValue); }