static NumberPrototype() { ValueReadOnly = MondPrototypeBinder.Bind(typeof(NumberPrototype)); ValueReadOnly.Prototype = ValuePrototype.Value; ValueReadOnly.Lock(); }
static ObjectPrototype() { Value = MondPrototypeBinder.Bind(typeof(ObjectPrototype)); Value.Prototype = ValuePrototype.Value; Value.Lock(); }
static StringPrototype() { Value = MondPrototypeBinder.Bind(typeof(StringPrototype)); Value.Prototype = ValuePrototype.Value; Value.Lock(); }
static ArrayPrototype() { Value = MondPrototypeBinder.Bind(typeof(ArrayPrototype)); Value.Prototype = ValuePrototype.Value; Value.Lock(); }
public static MondValue Lock([MondInstance] MondValue instance) { EnsureObject("lock", instance); instance.Lock(); return(instance); }
static FunctionPrototype() { Value = MondPrototypeBinder.Bind(typeof(FunctionPrototype)); Value.Prototype = ValuePrototype.Value; Value.Lock(); }
static ArrayPrototype() { ValueReadOnly = MondPrototypeBinder.Bind(typeof(ArrayPrototype)); ValueReadOnly.Prototype = ValuePrototype.Value; ValueReadOnly.Lock(); }
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(); }
static NumberPrototype() { Value = new MondValue(MondValueType.Object); Value.Prototype = ValuePrototype.Value; Value["isNaN"] = new MondInstanceFunction(IsNaN); Value.Lock(); }
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; }
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; }
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); }
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; }
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; }
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); }
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); }
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); }
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(); }
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(); }
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"); }
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"); }
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(); }
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); }
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; }