Пример #1
0
        public JSValue Invoke(JSContext ctx, JSValue this_obj, int argc, JSValue[] argv)
        {
            var self       = _delegate.Target;
            var methodInfo = _delegate.Method;
            var parameters = methodInfo.GetParameters();
            var nArgs      = argc;
            var args       = new object[nArgs];

            for (var i = 0; i < nArgs; i++)
            {
                if (!Values.js_get_var(ctx, argv[i], parameters[i].ParameterType, out args[i]))
                {
                    return(JSApi.JS_ThrowInternalError(ctx, "failed to cast val"));
                }
            }
            if (methodInfo.ReturnType != typeof(void))
            {
                var ret = methodInfo.Invoke(self, args);
                return(Values.js_push_var(ctx, ret));
            }
            else
            {
                methodInfo.Invoke(self, args);
                return(JSApi.JS_UNDEFINED);
            }
        }
Пример #2
0
        public override JSValue Invoke(JSContext ctx, JSValue this_obj, int argc, JSValue[] argv)
        {
            if (!_ctor.IsPublic && !_type.privateAccess)
            {
                return(JSApi.JS_ThrowInternalError(ctx, "constructor is inaccessible due to its protection level"));
            }

            var parameters = _ctor.GetParameters();
            var nArgs      = argc;
            var args       = new object[nArgs];

            for (var i = 0; i < nArgs; i++)
            {
                if (!Values.js_get_var(ctx, argv[i], parameters[i].ParameterType, out args[i]))
                {
                    return(JSApi.JS_ThrowInternalError(ctx, "failed to cast val"));
                }
            }

            if (_crossbind)
            {
                UnityEngine.Debug.LogFormat($"_js_crossbind_constructor {_type}");
                return(Values._js_crossbind_constructor(ctx, this_obj));
            }

            var inst = _ctor.Invoke(args);
            var val  = Values.NewBridgeClassObject(ctx, this_obj, inst, _type.id, false);

            return(val);
        }
Пример #3
0
        public JSValue GetValue(JSContext ctx, JSValue this_obj)
        {
            var propInfoGetMethod = _propertyInfo.GetGetMethod(true);

            if (propInfoGetMethod == null)
            {
                throw new NullReferenceException("property getter is null");
            }
            if (!propInfoGetMethod.IsPublic && !_type.privateAccess)
            {
                throw new InaccessibleMemberException(_propertyInfo.Name);
            }
            object self = null;

            if (!propInfoGetMethod.IsStatic)
            {
                if (!Values.js_get_var(ctx, this_obj, _type.type, out self) || !_type.CheckThis(self))
                {
                    throw new ThisBoundException();
                }
            }

            var rval = _propertyInfo.GetValue(self, null);

            return(Values.js_push_var(ctx, rval));
        }
Пример #4
0
        public JSValue SetValue(JSContext ctx, JSValue this_obj, JSValue val)
        {
            if (!_fieldInfo.IsPublic && !_type.privateAccess)
            {
                throw new InaccessibleMemberException(_fieldInfo.Name);
            }
            object self = null;

            if (!_fieldInfo.IsStatic)
            {
                if (!Values.js_get_var(ctx, this_obj, _type.type, out self) || !_type.CheckThis(self))
                {
                    throw new ThisBoundException();
                }
            }
            object t_val = null;

            if (!Values.js_get_var(ctx, val, _fieldInfo.FieldType, out t_val))
            {
                throw new InvalidCastException();
            }
            _fieldInfo.SetValue(self, t_val);

            if (_type.type.IsValueType && !_fieldInfo.IsStatic)
            {
                Values.js_rebind_var(ctx, this_obj, _type.type, self);
            }

            return(JSApi.JS_UNDEFINED);
        }
Пример #5
0
        public JSValue SetValue(JSContext ctx, JSValue this_obj, JSValue val)
        {
            var propInfoSetMethod = _propertyInfo.GetSetMethod(true);

            if (propInfoSetMethod == null)
            {
                throw new NullReferenceException("property setter is null");
            }
            if (!propInfoSetMethod.IsPublic && !_type.privateAccess)
            {
                throw new InaccessibleMemberException(_propertyInfo.Name);
            }
            object self = null;

            if (!propInfoSetMethod.IsStatic)
            {
                if (!Values.js_get_var(ctx, this_obj, _type.type, out self) || !_type.CheckThis(self))
                {
                    throw new ThisBoundException();
                }
            }
            object t_val = null;

            if (!Values.js_get_var(ctx, val, _propertyInfo.PropertyType, out t_val))
            {
                throw new InvalidCastException();
            }
            _propertyInfo.SetValue(self, t_val, null);
            if (_type.type.IsValueType && !propInfoSetMethod.IsStatic)
            {
                Values.js_rebind_var(ctx, this_obj, _type.type, self);
            }
            return(JSApi.JS_UNDEFINED);
        }
Пример #6
0
        public JSValue SetValue(JSContext ctx, JSValue this_obj, JSValue val)
        {
            if (!_fieldInfo.IsPublic && !_type.privateAccess)
            {
                return(JSApi.JS_ThrowInternalError(ctx, "field is inaccessible due to its protection level"));
            }
            object self = null;

            if (!_fieldInfo.IsStatic)
            {
                Values.js_get_cached_object(ctx, this_obj, out self);
                if (!_type.CheckThis(self))
                {
                    throw new ThisBoundException();
                }
            }
            object t_val = null;

            if (!Values.js_get_var(ctx, val, _fieldInfo.FieldType, out t_val))
            {
                return(JSApi.JS_ThrowInternalError(ctx, "failed to cast val"));
            }
            _fieldInfo.SetValue(self, t_val);
            return(JSApi.JS_UNDEFINED);
        }
Пример #7
0
        public JSValue GetValue(JSContext ctx, JSValue this_obj)
        {
            if (!_fieldInfo.IsPublic && !_type.privateAccess)
            {
                throw new InaccessibleMemberException(_fieldInfo.Name);
            }
            object self = null;

            if (!_fieldInfo.IsStatic)
            {
                if (!Values.js_get_var(ctx, this_obj, _type.type, out self) || !_type.CheckThis(self))
                {
                    throw new ThisBoundException();
                }
            }

            var rval = _fieldInfo.GetValue(self);

            return(Values.js_push_var(ctx, rval));
        }
Пример #8
0
        public override JSValue Invoke(JSContext ctx, JSValue this_obj, int argc, JSValue[] argv)
        {
            if (!_methodInfo.IsPublic && !_type.privateAccess)
            {
                return(JSApi.JS_ThrowInternalError(ctx, "method is inaccessible due to its protection level"));
            }
            object self = null;

            if (!_methodInfo.IsStatic)
            {
                Values.js_get_cached_object(ctx, this_obj, out self);
                if (!_type.CheckThis(self))
                {
                    throw new ThisBoundException();
                }
            }
            var parameters = _methodInfo.GetParameters();
            var nArgs      = argc;
            var args       = new object[nArgs];

            for (var i = 0; i < nArgs; i++)
            {
                if (!Values.js_get_var(ctx, argv[i], parameters[i].ParameterType, out args[i]))
                {
                    return(JSApi.JS_ThrowInternalError(ctx, "failed to cast val"));
                }
            }
            if (_methodInfo.ReturnType != typeof(void))
            {
                var ret = _methodInfo.Invoke(self, args);
                return(Values.js_push_var(ctx, ret));
            }
            else
            {
                _methodInfo.Invoke(self, args);
                return(JSApi.JS_UNDEFINED);
            }
        }
Пример #9
0
        public override JSValue Invoke(JSContext ctx, JSValue this_obj, int argc, JSValue[] argv)
        {
            if (!_ctor.IsPublic && !_type.privateAccess)
            {
                return(JSApi.JS_ThrowInternalError(ctx, "constructor is inaccessible due to its protection level"));
            }

            var nArgs = argc;
            var args  = new object[nArgs];

            for (var i = 0; i < nArgs; i++)
            {
                if (!Values.js_get_var(ctx, argv[i], _parameters[i].ParameterType, out args[i]))
                {
                    return(JSApi.JS_ThrowInternalError(ctx, "failed to cast val"));
                }
            }

            var inst = _ctor.Invoke(args);
            var val  = Values.js_new_var(ctx, this_obj, _type.type, inst, _type.id, _disposable);

            return(val);
        }
Пример #10
0
        public override JSValue Invoke(JSContext ctx, JSValue this_obj, int argc, JSValue[] argv)
        {
            if (!_methodInfo.IsPublic && !_type.privateAccess)
            {
                return(JSApi.JS_ThrowInternalError(ctx, "method is inaccessible due to its protection level"));
            }
            object self = null;

            if (!_methodInfo.IsStatic)
            {
                if (!Values.js_get_var(ctx, this_obj, _type.type, out self) || !_type.CheckThis(self))
                {
                    throw new ThisBoundException();
                }
            }
            var nArgs       = _methodParameters.Length;
            var args        = new object[nArgs];
            var vIndex      = 0;
            var bBackValues = false;

            for (var i = 0; i < nArgs; i++)
            {
                var parameterInfo = _methodParameters[i];
                var pType         = parameterInfo.ParameterType;
                if (Values.IsContextualType(pType))
                {
                    args[i] = Values.js_get_context(ctx, pType);
                }
                else
                {
                    if (_isVarargMethod && i == nArgs - 1)
                    {
                        var varArgLength = argc - nArgs + 1;
                        var varArgType   = pType.GetElementType();
                        var varArgArray  = Array.CreateInstance(varArgType, varArgLength);
                        for (var varArgIndex = 0; varArgIndex < varArgLength; varArgIndex++)
                        {
                            object varArgElement = null;
                            if (!Values.js_get_var(ctx, argv[vIndex++], varArgType, out varArgElement))
                            {
                                return(JSApi.JS_ThrowInternalError(ctx, $"failed to cast val vararg #{varArgIndex}"));
                            }
                            varArgArray.SetValue(varArgElement, varArgIndex);
                        }
                        args[i] = varArgArray;
                    }
                    else
                    {
                        if (pType.IsByRef)
                        {
                            bBackValues = true;
                            if (!parameterInfo.IsOut)
                            {
                                if (!Values.js_get_var(ctx, argv[vIndex], pType.GetElementType(), out args[i]))
                                {
                                    return(JSApi.JS_ThrowInternalError(ctx, $"failed to cast val byref #{vIndex}"));
                                }
                            }
                        }
                        else
                        {
                            if (!Values.js_get_var(ctx, argv[vIndex], pType, out args[i]))
                            {
                                return(JSApi.JS_ThrowInternalError(ctx, $"failed to cast val #{vIndex}"));
                            }
                        }
                        vIndex++;
                    }
                }
            }

            var ret = _methodInfo.Invoke(self, args);

            if (bBackValues)
            {
                vIndex = 0;
                for (var i = 0; i < nArgs; i++)
                {
                    var parameterInfo = _methodParameters[i];
                    var pType         = parameterInfo.ParameterType;
                    if (!Values.IsContextualType(pType))
                    {
                        if (_isVarargMethod && i == nArgs - 1)
                        {
                        }
                        else
                        {
                            if (pType.IsByRef)
                            {
                                var backValue = Values.js_push_var(ctx, args[i]);
                                var valueAtom = ScriptEngine.GetContext(ctx).GetAtom("value");
                                JSApi.JS_SetProperty(ctx, argv[vIndex], valueAtom, backValue);
                            }

                            vIndex++;
                        }
                    }
                }
            }

            if (_type.type.IsValueType && !_methodInfo.IsStatic)
            {
                Values.js_rebind_var(ctx, this_obj, _type.type, self);
            }

            if (_methodInfo.ReturnType != typeof(void))
            {
                return(Values.js_push_var(ctx, ret));
            }

            return(JSApi.JS_UNDEFINED);
        }