Example #1
0
        public async Task NKinjectScript(NKScriptSource source)
        {
            if (_injectedScripts.Contains(source))
            {
                throw new InvalidOperationException("Script has already been injected to a context;  create separate NKSCriptSource for each instance");
            }

            _injectedScripts.Add(source);
            source.registerInject(this);
            await InjectScript(source);

            NKLogging.log(string.Format("+E{0} Injected {1}", this.NKid, source.filename));
        }
        // Public methods
        public virtual async Task <NKScriptValue> bindPlugin <T>(T obj, string ns) where T : class
        {
            var context = this.context;

            context.setNKScriptChannel(this);
            if ((this.id != null) || (context == null))
            {
                return(null);
            }

            this.id = (NKScriptChannel.sequenceNumber++).ToString();

            context.NKaddScriptMessageHandler(this, id);

            string name;

            if (typeof(T) == typeof(Type))
            {
                // Class, not instance, passed to bindPlugin -- to be used in Factory constructor/instance pattern in js
                isFactory = true;
                _typeInfo = new NKScriptTypeInfo <T>(obj);
                name      = (obj as Type).Name;
                // Need to store the channel on the class itself so it can be found when native construct requests come in from other plugins
                obj.setNKScriptChannel(this);
            }
            else
            {
                name = (typeof(T)).Name;
                // Instance of Princpal passed to bindPlugin -- to be used in singleton/static pattern in js
                isFactory = false;
                _typeInfo = new NKScriptTypeInfo <T>(obj);
            }

            _principal         = new NKScriptValueNative(ns, this, 0, obj);
            this._instances[0] = _principal;
            _channels[ns]      = this;
            obj.setNKScriptValue(_principal);

            var export = new NKScriptExportProxy <T>(obj);

            var script = new NKScriptSource(_generateStubs(export, name), ns + "/plugin/" + name + ".js");
            await context.NKinjectScript(script);

            await export.initializeForContext(context);

            return(_principal);
        }
Example #3
0
        public async Task <NKScriptValue> bindPlugin(object obj, string ns)
        {
            await this.prepareForPlugin();

            if ((this.id != null) || (context == null))
            {
                return(null);
            }
            if (this.userContentController == null)
            {
                return(null);
            }

            this.id = (this.sequenceNumber++).ToString();

            this.userContentController.NKaddScriptMessageHandler(this, id);

            if (obj.GetType() == typeof(Type))
            {
                // Class, not instance, passed to bindPlugin -- to be used in Factory constructor/instance pattern in js
                isFactory = true;
                typeInfo  = new NKScriptTypeInfo((Type)obj);
                // Need to store the channel on the class itself so it can be found when native construct requests come in from other plugins
                obj.setNKScriptChannel(this);
            }
            else
            {
                // Instance of Princpal passed to bindPlugin -- to be used in singleton/static pattern in js
                isFactory = false;
                typeInfo  = new NKScriptTypeInfo(obj.GetType());
            }

            _principal        = new NKScriptValueNative(ns, this, obj);
            this.instances[0] = _principal;

            var script = new NKScriptSource(_generateStubs(typeInfo.Name), ns + "/plugin/" + typeInfo.Name + ".js");

            NKLogging.log(script.source);
            await script.inject(context);

            return(_principal);
        }
Example #4
0
        async private Task prepareForPlugin()
        {
            if (preparationComplete)
            {
                return;
            }
            try
            {
                context.setNKScriptChannel(this);
                var source = await getResource("nkscripting.js", "lib");

                if (source == null)
                {
                    throw new FileNotFoundException("Could not find file nkscripting.js");
                }

                var script = new NKScriptSource(source, "io.nodekit.scripting/NKScripting/nkscripting.js", "NKScripting", null);
                await script.inject(context);

                preparationComplete = true;
            }
            catch
            {
                preparationComplete = false;
            }

            var source2 = await getResource("promise.js", "lib");

            var script2 = new NKScriptSource(source2, "io.nodekit.scripting/NKScripting/promise.js", "Promise", null);
            await script2.inject(context);

            if (preparationComplete)
            {
                NKLogging.log(String.Format("+E{0} JavaScript Engine is ready for loading plugins", context.NKid));
            }
            else
            {
                NKLogging.log(String.Format("+E{0} JavaScript Engine could not load NKScripting.js", context.NKid));
            }
        }
Example #5
0
        async public Task <NKScriptContext> completeInitialization()
        {
            if (preparationComplete)
            {
                return(this);
            }
            try
            {
                var source = await NKStorage.getResourceAsync(typeof(NKScriptContext), "nkscripting.js", "lib");

                if (source == null)
                {
                    throw new FileNotFoundException("Could not find file nkscripting.js");
                }

                var script = new NKScriptSource(source, "io.nodekit.scripting/NKScripting/nkscripting.js", "NKScripting", null);
                await this.NKinjectScript(script);
                await ensureOnEngineThread(async() =>
                {
                    await PrepareEnvironment();
                    preparationComplete = true;
                }).Unwrap();
            }
            catch
            {
                preparationComplete = false;
            }

            if (preparationComplete)
            {
                NKLogging.log(String.Format("+E{0} JavaScript Engine is ready for loading plugins", this.NKid));
            }
            else
            {
                NKLogging.log(String.Format("+E{0} JavaScript Engine could not load NKScripting.js", this.NKid));
            }

            return(this);
        }
Example #6
0
 protected abstract Task InjectScript(NKScriptSource source);