public static GearsNativeWrapper GetWrapper(Type type)
 {
     if (_Wrappers.TryGetValue(type, out GearsNativeWrapper wrapper))
     {
         return(wrapper);
     }
     try {
         wrapper = new GearsNativeWrapper(type);
     }
     catch (Exception e) {
         throw new GearsRuntimeException($"GearsNativeWrapper.GetWrapper: Could not wrap object of type {type.Name}. Inner error: {e.Message}");
     }
     _Wrappers[type] = wrapper;
     return(wrapper);
 }
Beispiel #2
0
        internal bool CallGearsFunction(ulong name, out object returned, params object[] args)
        {
            if (!Globals.TryGet(name, out GearsValue fnValue) || !fnValue.IsObjPtr)
            {
                // error: no function with that name.
                returned = $"Error: no function with name '{BitString.GetBitStr(name)}'.";
                return(false);
            }
            GearsObj fnObject = fnValue.AsObject(this);

            if (fnObject is GearsObjFunction fnFunction)
            {
                if (fnFunction.Arity != args.Length)
                {
                    // error: wrong arity.
                    returned = $"Error: called '{BitString.GetBitStr(name)}' with wrong arity (passed arity is '{args?.Length ?? 0}').";
                    return(false);
                }
            }
            Push(fnValue);
            for (int i = 0; i < (args?.Length ?? 0); i++)
            {
                object arg     = args[i];
                Type   argType = arg?.GetType() ?? null;
                if (arg == null)
                {
                    Push(GearsValue.NilValue);
                }
                else if (GearsNativeWrapper.IsNumeric(argType))
                {
                    double fieldValue = Convert.ToDouble(arg);
                    Push(new GearsValue(fieldValue));
                }
                else if (argType == typeof(bool))
                {
                    bool fieldValue = Convert.ToBoolean(arg);
                    Push(fieldValue ? GearsValue.TrueValue : GearsValue.FalseValue);
                }
                else if (argType == typeof(string))
                {
                    string fieldValue = Convert.ToString(arg);
                    if (fieldValue == null)
                    {
                        Push(GearsValue.NilValue);
                    }
                    else
                    {
                        Push(GearsValue.CreateObjPtr(HeapAddObject(new GearsObjString(fieldValue))));
                    }
                }
                else if (argType.IsSubclassOf(typeof(object)))
                {
                    if (arg == null)
                    {
                        Push(GearsValue.NilValue);
                    }
                    else
                    {
                        Push(GearsValue.CreateObjPtr(HeapAddObject(new GearsObjInstanceNative(this, arg))));
                    }
                }
                else
                {
                    // error: could not pass arg of this type
                    returned = $"Error: called '{BitString.GetBitStr(name)}' with argument of type '{argType.Name}' as parameter {i}. Gears could not interpret this argument.";
                    return(false);
                }
            }
            Call(args?.Length ?? 0);
            Run();
            returned = LastReturnValue; // the return value
            // todo: process return value?
            return(true);
        }