Esempio n. 1
0
        private async Task <Dictionary <string, object> > RunInlineVm(string[] runtimeArgs, string extensionArgsJson, CbxBundle cbxBundle, bool showLibStack, bool showOutputPrefixes, Interpreter.ResourceReader resourceReader)
        {
            string byteCode         = cbxBundle.ByteCode;
            string resourceManifest = cbxBundle.ResourceDB.ResourceManifestFile.TextContent;
            string imageManifest    = cbxBundle.ResourceDB.ImageResourceManifestFile.TextContent;

            Interpreter.Structs.VmContext vm = Interpreter.Vm.CrayonWrapper.createVm(byteCode, resourceManifest, imageManifest);
            resourceReader.Vm = vm;

            Interpreter.Vm.CrayonWrapper.vmEnvSetCommandLineArgs(vm, runtimeArgs);
            Interpreter.Vm.CrayonWrapper.vmSetResourceReaderObj(vm, resourceReader);
            Interpreter.Vm.CrayonWrapper.vmSetWaxHub(vm, this.Hub);
            Interpreter.Vm.CrayonWrapper.vmSetWaxPayload(vm, extensionArgsJson);

            vm.environment.stdoutPrefix     = showOutputPrefixes ? "STDOUT" : null;
            vm.environment.stacktracePrefix = showOutputPrefixes ? "STACKTRACE" : null;

            if (showLibStack)
            {
                Interpreter.Vm.CrayonWrapper.vmEnableLibStackTrace(vm);
            }
            Interpreter.Vm.EventLoop eventLoop = new Interpreter.Vm.EventLoop(vm);

            int vmId = vmIdAlloc++;

            Interpreter.Vm.CrayonWrapper.setVmId(vm, vmId);
            vmContexts[vmId] = vm;

            await eventLoop.StartInterpreter();

            string response = Interpreter.Vm.CrayonWrapper.vmGetWaxResponse(vm) ?? "{}";

            return(new Dictionary <string, object>(new Wax.Util.JsonParser(response).ParseAsDictionary()));
        }
Esempio n. 2
0
 internal static EventLoop GetEventLoop(Interpreter.Structs.VmContext vm)
 {
     return((EventLoop)CrayonWrapper.vmGetEventLoopObj(vm));
 }
Esempio n. 3
0
        /*
         *  Called by a CNI function after the frame is shown and just blocks and waits for
         *  events from the queue.
         *  If a window close event is generated, then blocking ends.
         */
        public static void EventWatcher(
            Interpreter.Structs.VmContext vmContext,
            int resumingExecutionContextId,
            Interpreter.Structs.Value openedFrameAsValue,
            Interpreter.Structs.Value noriHandlerFunctionPointer,
            Interpreter.Structs.Value postShowFunctionPointer)
        {
            Interpreter.Vm.CrayonWrapper.runInterpreterWithFunctionPointer(
                vmContext,
                postShowFunctionPointer,
                new Interpreter.Structs.Value[] { openedFrameAsValue });

            // TODO: find a better way.
            while (activeFrames.Count > 0)
            {
                object[] events = null;
                lock (eventQueue)
                {
                    if (eventQueue.Count > 0)
                    {
                        events = eventQueue.ToArray();
                        eventQueue.Clear();
                    }
                }

                if (events != null)
                {
                    for (int i = 0; i < events.Length; i += 5)
                    {
                        string    type   = events[i].ToString();
                        NoriFrame sender = (NoriFrame)events[i + 1];
                        if (type == "EVENT")
                        {
                            int    elementId = (int)events[i + 2];
                            string eventName = (string)events[i + 3];
                            string args      = (string)events[i + 4];
                            Interpreter.Vm.CrayonWrapper.runInterpreterWithFunctionPointer(
                                vmContext,
                                noriHandlerFunctionPointer,
                                new Interpreter.Structs.Value[] {
                                sender.CrayonObjectRef,
                                Interpreter.Vm.CrayonWrapper.buildInteger(vmContext.globals, elementId),
                                Interpreter.Vm.CrayonWrapper.buildString(vmContext.globals, eventName),
                                Interpreter.Vm.CrayonWrapper.buildString(vmContext.globals, args)
                            });
                        }
                        else if (type == "CLOSE")
                        {
                            if (activeFrames.Contains(sender))
                            {
                                activeFrames.Remove(sender);
                            }

                            if (resumingExecutionContextId != sender.ResumeExecId)
                            {
                                Interpreter.Vm.CrayonWrapper.runInterpreter(vmContext, sender.ResumeExecId);
                            }
                        }
                    }
                }
                else
                {
                    System.Threading.Thread.Sleep(System.TimeSpan.FromMilliseconds(0.1));
                }
            }

            Structs.ExecutionContext ec = Interpreter.Vm.CrayonWrapper.getExecutionContext(vmContext, resumingExecutionContextId);
            ec.activeInterrupt      = null;
            ec.executionStateChange = false;
        }