示例#1
0
        static NumberPrototype()
        {
            ValueReadOnly           = MondPrototypeBinder.Bind(typeof(NumberPrototype));
            ValueReadOnly.Prototype = ValuePrototype.Value;

            ValueReadOnly.Lock();
        }
示例#2
0
        static ObjectPrototype()
        {
            Value = MondPrototypeBinder.Bind(typeof(ObjectPrototype));
            Value.Prototype = ValuePrototype.Value;

            Value.Lock();
        }
示例#3
0
        static StringPrototype()
        {
            Value = MondPrototypeBinder.Bind(typeof(StringPrototype));
            Value.Prototype = ValuePrototype.Value;

            Value.Lock();
        }
示例#4
0
        static ArrayPrototype()
        {
            Value           = MondPrototypeBinder.Bind(typeof(ArrayPrototype));
            Value.Prototype = ValuePrototype.Value;

            Value.Lock();
        }
示例#5
0
        static ObjectPrototype()
        {
            Value           = MondPrototypeBinder.Bind(typeof(ObjectPrototype));
            Value.Prototype = ValuePrototype.Value;

            Value.Lock();
        }
示例#6
0
        public static MondValue Lock([MondInstance] MondValue instance)
        {
            EnsureObject("lock", instance);

            instance.Lock();
            return(instance);
        }
示例#7
0
        static StringPrototype()
        {
            Value           = MondPrototypeBinder.Bind(typeof(StringPrototype));
            Value.Prototype = ValuePrototype.Value;

            Value.Lock();
        }
示例#8
0
        static FunctionPrototype()
        {
            Value           = MondPrototypeBinder.Bind(typeof(FunctionPrototype));
            Value.Prototype = ValuePrototype.Value;

            Value.Lock();
        }
示例#9
0
        static ArrayPrototype()
        {
            ValueReadOnly           = MondPrototypeBinder.Bind(typeof(ArrayPrototype));
            ValueReadOnly.Prototype = ValuePrototype.Value;

            ValueReadOnly.Lock();
        }
示例#10
0
        static ValuePrototype()
        {
            Value = MondPrototypeBinder.Bind(typeof(ValuePrototype));

            // we dont use MondValue.Prototype here because this should not have a prototype
            Value.ObjectValue.Prototype = MondValue.Undefined;

            Value.Lock();
        }
示例#11
0
        static ValuePrototype()
        {
            Value = MondPrototypeBinder.Bind(typeof(ValuePrototype));

            // we dont use MondValue.Prototype here because this should not have a prototype
            Value.ObjectValue.Prototype = MondValue.Undefined;

            Value.Lock();
        }
示例#12
0
        static NumberPrototype()
        {
            Value           = new MondValue(MondValueType.Object);
            Value.Prototype = ValuePrototype.Value;

            Value["isNaN"] = new MondInstanceFunction(IsNaN);

            Value.Lock();
        }
示例#13
0
        static ValuePrototype()
        {
            Value           = new MondValue(MondValueType.Object);
            Value.Prototype = MondValue.Null; // required to break the chain

            Value["getType"]   = new MondInstanceFunction(GetType);
            Value["toString"]  = new MondInstanceFunction(ToString);
            Value["serialize"] = new MondInstanceFunction(Serialize);
            Value.Lock();
        }
        public MondValue ToMond( MondState state )
        {
            MondValue prototype;
            MondClassBinder.Bind<NamespaceReference>( out prototype, state );

            var obj = new MondValue( state );
            obj.UserData = this;
            obj.Prototype = prototype;
            obj.Lock();

            return obj;
        }
示例#15
0
        public static MondValue Create()
        {
            MondValue prototype;
            MondClassBinder.Bind<AsyncClass>(out prototype);

            var instance = new AsyncClass();

            var obj = new MondValue(MondValueType.Object);
            obj.UserData = instance;
            obj.Prototype = prototype;
            obj.Lock();

            return obj;
        }
示例#16
0
        public MondValue ToMond(MondState state)
        {
            MondClassBinder.Bind <NamespaceReference>(state, out var prototype);

            var obj = new MondValue(state)
            {
                UserData  = this,
                Prototype = prototype
            };

            obj.Lock();

            return(obj);
        }
示例#17
0
        public static MondValue Create(RequireLibrary require)
        {
            MondValue prototype;
            MondClassBinder.Bind<RequireClass>(out prototype);

            var instance = new RequireClass();
            instance._require = require;

            var obj = new MondValue(MondValueType.Object);
            obj.UserData = instance;
            obj.Prototype = prototype;
            obj.Lock();

            return obj;
        }
示例#18
0
        public static MondValue Create(ConsoleOutputLibrary consoleOutput)
        {
            MondValue prototype;
            MondClassBinder.Bind<ConsoleOutputClass>(out prototype);

            var instance = new ConsoleOutputClass();
            instance._consoleOutput = consoleOutput;

            var obj = new MondValue(MondValueType.Object);
            obj.UserData = instance;
            obj.Prototype = prototype;
            obj.Lock();

            return obj;
        }
示例#19
0
        public static MondValue Create(MondState state, RequireLibrary require)
        {
            MondClassBinder.Bind <RequireClass>(state, out var prototype);

            var instance = new RequireClass();

            instance._require = require;

            var obj = new MondValue(MondValueType.Object);

            obj.UserData  = instance;
            obj.Prototype = prototype;
            obj.Lock();

            return(obj);
        }
示例#20
0
        public static MondValue Create(MondState state)
        {
            MondClassBinder.Bind <AsyncClass>(state, out var prototype);

            var instance = new AsyncClass();

            var obj = new MondValue(MondValueType.Object)
            {
                UserData  = instance,
                Prototype = prototype
            };

            obj.Lock();

            return(obj);
        }
示例#21
0
        public static MondValue Create(MondState state, ConsoleInputLibrary consoleInput)
        {
            MondValue prototype;

            MondClassBinder.Bind <ConsoleInputClass>(state, out prototype);

            var instance = new ConsoleInputClass();

            instance._consoleInput = consoleInput;

            var obj = new MondValue(MondValueType.Object);

            obj.UserData  = instance;
            obj.Prototype = prototype;
            obj.Lock();

            return(obj);
        }
示例#22
0
        static ObjectPrototype()
        {
            Value           = new MondValue(MondValueType.Object);
            Value.Prototype = ValuePrototype.Value;

            Value["add"]           = new MondInstanceFunction(Add);
            Value["clear"]         = new MondInstanceFunction(Clear);
            Value["containsKey"]   = new MondInstanceFunction(ContainsKey);
            Value["containsValue"] = new MondInstanceFunction(ContainsValue);
            Value["get"]           = new MondInstanceFunction(Get);
            Value["remove"]        = new MondInstanceFunction(Remove);

            Value["length"]        = new MondInstanceFunction(Length);
            Value["getEnumerator"] = new MondInstanceFunction(GetEnumerator);

            Value["prototype"] = new MondInstanceFunction(Prototype);

            Value.Lock();
        }
示例#23
0
        static ArrayPrototype()
        {
            Value           = new MondValue(MondValueType.Object);
            Value.Prototype = ValuePrototype.Value;

            Value["add"]         = new MondInstanceFunction(Add);
            Value["clear"]       = new MondInstanceFunction(Clear);
            Value["contains"]    = new MondInstanceFunction(Contains);
            Value["indexOf"]     = new MondInstanceFunction(IndexOf);
            Value["lastIndexOf"] = new MondInstanceFunction(LastIndexOf);
            Value["insert"]      = new MondInstanceFunction(Insert);
            Value["remove"]      = new MondInstanceFunction(Remove);
            Value["removeAt"]    = new MondInstanceFunction(RemoveAt);

            Value["length"]        = new MondInstanceFunction(Length);
            Value["getEnumerator"] = new MondInstanceFunction(GetEnumerator);

            Value.Lock();
        }
示例#24
0
        public void Lock()
        {
            var obj       = new MondValue(MondValueType.Object);
            var prototype = obj.Prototype;

            Assert.True(obj["getType"].Type == MondValueType.Function, "no getType");

            obj["getType"] = 123;

            Assert.True(prototype["getType"].Type == MondValueType.Function, "set wrong field on locked prototype");
            Assert.True(obj["getType"] == 123, "set on locked prototype");

            Assert.Throws <MondRuntimeException>(() => prototype["getType"] = 123, "set on locked object");

            Assert.True(obj["test"] == MondValue.Undefined);

            obj.Lock();

            Assert.Throws <MondRuntimeException>(() => obj["test"] = 123, "create on locked object");
        }
示例#25
0
        public void ObjectPrototype()
        {
            var prototype = new MondValue(MondValueType.Object);
            var obj       = new MondValue(MondValueType.Object);

            obj.Prototype = prototype;
            Assert.True(obj.Prototype.Type == MondValueType.Object);

            prototype["testValue"] = "hello";

            Assert.True(obj["testValue"] == "hello");
            Assert.True(obj["containsKey"].Type == MondValueType.Function);

            obj.Prototype = MondValue.Null; // no prototype

            Assert.True(obj["testValue"] == MondValue.Undefined);
            Assert.True(obj["containsKey"] == MondValue.Undefined);

            obj.Lock();
            Assert.Throws <MondRuntimeException>(() => obj.Prototype = null, "modify locked object prototype");
        }
示例#26
0
        static StringPrototype()
        {
            Value           = new MondValue(MondValueType.Object);
            Value.Prototype = ValuePrototype.Value;

            Value["charAt"]      = new MondInstanceFunction(CharAt);
            Value["contains"]    = new MondInstanceFunction(Contains);
            Value["endsWith"]    = new MondInstanceFunction(EndsWith);
            Value["indexOf"]     = new MondInstanceFunction(IndexOf);
            Value["insert"]      = new MondInstanceFunction(Insert);
            Value["lastIndexOf"] = new MondInstanceFunction(LastIndexOf);
            Value["replace"]     = new MondInstanceFunction(Replace);
            Value["split"]       = new MondInstanceFunction(Split);
            Value["startsWith"]  = new MondInstanceFunction(StartsWith);
            Value["substring"]   = new MondInstanceFunction(Substring);
            Value["toUpper"]     = new MondInstanceFunction(ToUpper);
            Value["toLower"]     = new MondInstanceFunction(ToLower);
            Value["trim"]        = new MondInstanceFunction(Trim);

            Value["length"]        = new MondInstanceFunction(Length);
            Value["getEnumerator"] = new MondInstanceFunction(GetEnumerator);

            Value.Lock();
        }
示例#27
0
        public MondValue Require(MondState state, string fileName)
        {
            if (_require.Loader == null)
            {
                throw new MondRuntimeException("require: module loader is not set");
            }

            const string cacheObjectName = "__modules";

            MondValue cacheObject;

            // make sure we have somewhere to cache modules
            if (state[cacheObjectName].Type != MondValueType.Object)
            {
                cacheObject            = new MondValue(state);
                cacheObject.Prototype  = MondValue.Null;
                state[cacheObjectName] = cacheObject;
            }
            else
            {
                cacheObject = state[cacheObjectName];
            }

            // return cached module if it exists
            var cachedExports = cacheObject[fileName];

            if (cachedExports.Type == MondValueType.Object)
            {
                return(cachedExports);
            }

            // create a new object to store the exports
            var exports = new MondValue(state);

            exports.Prototype = MondValue.Null;

            // instantly cache it so we can have circular dependencies
            cacheObject[fileName] = exports;

            try
            {
                IEnumerable <string> searchDirectories =
                    _require.SearchDirectories ?? Array.Empty <string>();

                if (_require.SearchBesideScript)
                {
                    var currentDir = Path.GetDirectoryName(state.CurrentScript);
                    searchDirectories = Enumerable.Repeat(currentDir, 1)
                                        .Concat(searchDirectories);
                }

                var moduleSource = _require.Loader(fileName, searchDirectories);

                // wrap the module script in a function so we can pass out exports object to it
                var source = _require.Definitions + "return fun (exports) {\n" + moduleSource + " return exports; };";

                var options = new MondCompilerOptions
                {
                    FirstLineNumber = -1
                };

                var requireOptions = _require.Options;
                if (requireOptions != null)
                {
                    options.DebugInfo = requireOptions.DebugInfo;
                    options.MakeRootDeclarationsGlobal = requireOptions.MakeRootDeclarationsGlobal;
                    options.UseImplicitGlobals         = requireOptions.UseImplicitGlobals;
                }

                var program     = MondProgram.Compile(source, fileName, options);
                var initializer = state.Load(program);

                var result = state.Call(initializer, exports);

                if (result.Type != MondValueType.Object)
                {
                    throw new MondRuntimeException("require: module must return an object (`{0}`)", fileName);
                }

                if (!ReferenceEquals(exports, result))
                {
                    // module returned a different object, merge with ours
                    foreach (var kv in result.Object)
                    {
                        var key   = kv.Key;
                        var value = kv.Value;

                        exports[key] = value;
                    }

                    exports.Prototype = result.Prototype;

                    if (result.IsLocked)
                    {
                        exports.Lock();
                    }
                }
            }
            catch
            {
                // if something goes wrong, remove the entry from the cache
                cacheObject[fileName] = MondValue.Undefined;
                throw;
            }

            return(exports);
        }
示例#28
0
        public MondValue Require(MondState state, string fileName)
        {
            if (_require.Loader == null)
                throw new MondRuntimeException("require: module loader is not set");

            const string cacheObjectName = "__modules";

            MondValue cacheObject;

            // make sure we have somewhere to cache modules
            if (state[cacheObjectName].Type != MondValueType.Object)
            {
                cacheObject = new MondValue(state);
                cacheObject.Prototype = MondValue.Null;
                state[cacheObjectName] = cacheObject;
            }
            else
            {
                cacheObject = state[cacheObjectName];
            }

            // return cached module if it exists
            var cachedExports = cacheObject[fileName];
            if (cachedExports.Type == MondValueType.Object)
                return cachedExports;

            // create a new object to store the exports
            var exports = new MondValue(state);
            exports.Prototype = MondValue.Null;

            // instantly cache it so we can have circular dependencies
            cacheObject[fileName] = exports;

            try
            {
                var searchDirectories = new[] { Path.GetDirectoryName(state.CurrentScript), "" };
                var moduleSource = _require.Loader(fileName, searchDirectories.AsReadOnly());

                // wrap the module script in a function so we can pass out exports object to it
                var source = _require.Definitions + "return fun (exports) {\n" + moduleSource + " return exports; };";

                var options = new MondCompilerOptions
                {
                    FirstLineNumber = -1
                };

                var requireOptions = _require.Options;
                if (requireOptions != null)
                {
                    options.DebugInfo = requireOptions.DebugInfo;
                    options.MakeRootDeclarationsGlobal = requireOptions.MakeRootDeclarationsGlobal;
                    options.UseImplicitGlobals = requireOptions.UseImplicitGlobals;
                }

                var program = MondProgram.Compile(source, fileName, options);
                var initializer = state.Load(program);

                var result = state.Call(initializer, exports);

                if (result.Type != MondValueType.Object)
                    throw new MondRuntimeException("require: module must return an object (`{0}`)", fileName);

                if (!ReferenceEquals(exports, result))
                {
                    // module returned a different object, merge with ours
                    foreach (var kv in result.Object)
                    {
                        var key = kv.Key;
                        var value = kv.Value;

                        exports[key] = value;
                    }

                    exports.Prototype = result.Prototype;

                    if (result.IsLocked)
                        exports.Lock();
                }
            }
            catch
            {
                // if something goes wrong, remove the entry from the cache
                cacheObject[fileName] = MondValue.Undefined;
                throw;
            }

            return exports;
        }