예제 #1
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);
        }
예제 #2
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));
            }
        }