示例#1
0
        public override void GenerateFunctionGroup(List <Function> group)
        {
            var function = group.First();

            if (function.IsOperator)
            {
                return;
            }

            PushBlock(BlockKind.Function);
            {
                var callbacks = new QuickJSInvokes(Context);
                callbacks.GenerateFunctionGroup(group);
                Write(callbacks.Generate());
            }
            PopBlock(NewLineKind.BeforeNextBlock);

            PushBlock(BlockKind.Function);
            {
                Write($"static void register_function_{GetCIdentifier(Context, function)}");
                WriteLine("(JSContext *ctx, JSModuleDef *m, bool set)");
                WriteOpenBraceAndIndent();

                WriteLine("if (!set)");
                WriteOpenBraceAndIndent();
                WriteLine($"int status = JS_AddModuleExport(ctx, m, \"{function.Name}\");");
                WriteLine("assert(status != -1);");
                WriteLine("return;");
                UnindentAndWriteCloseBrace();
                NewLine();

                var callbackId = $"callback_function_{GetCIdentifier(Context, function)}";
                var maxParams  = @group.Max(f => f.Parameters.Count);

                WriteLine($"JSValue val = JS_NewCFunction(ctx, {callbackId}, \"{function.Name}\"," +
                          $" {maxParams});");
                WriteLine($"int status = JS_SetModuleExport(ctx, m, \"{function.Name}\", val);");
                WriteLine("assert(status != -1);");

                UnindentAndWriteCloseBrace();
            }
            PopBlock(NewLineKind.BeforeNextBlock);
        }
示例#2
0
        public override bool VisitClassDecl(Class @class)
        {
            if (@class.IsIncomplete)
            {
                return(true);
            }

            if (!VisitDeclContext(@class))
            {
                return(true);
            }

            PushBlock(BlockKind.InternalsClass, @class);
            {
                WriteLine($"JSClassID classId_{GetCIdentifier(Context, @class)};");
                NewLine();

                GenerateClassExtraData(@class);

                PushBlock();
                {
                    var callbacks = new QuickJSInvokes(Context);
                    @class.Visit(callbacks);
                    callbacks.GenerateToString(@class);
                    Write(callbacks.Generate());
                }
                PopBlock(NewLineKind.BeforeNextBlock);

                var finalizerId = $"finalizer_{GetCIdentifier(Context, @class)}";
                PushBlock();
                {
                    WriteLine($"void {finalizerId}(JSRuntime *rt, JSValue val)");
                    WriteOpenBraceAndIndent();

                    //WriteLine($"printf(\"Calling finalizer for {@class.QualifiedOriginalName}\\n\");");

                    if (ClassNeedsExtraData(@class))
                    {
                        // Remove the event connection from the delegate.
                        // var invokeId = $"event_invoke_{@event.OriginalName}";
                        // WriteLine($"data->instance->{@event.OriginalName}.bind(data, &{classDataId}::{invokeId});");
                        // NewLine();

                        var instanceKind = "JS_INTEROP_INSTANCE_SIGNAL_CONTEXT";
                        WriteLine($"JS_Interop_CleanupObject(val, {instanceKind});");
                    }
                    else
                    {
                        Write($"{@class.QualifiedOriginalName}* instance = ");
                        WriteLine($"({@class.QualifiedOriginalName}*) JS_GetOpaque(val, 0);");
                    }

                    UnindentAndWriteCloseBrace();
                }
                PopBlock(NewLineKind.BeforeNextBlock);

                PushBlock();
                {
                    WriteLine($"static JSClassDef classDef_{GetCIdentifier(Context, @class)}");
                    WriteOpenBraceAndIndent();

                    WriteLine($"\"{@class.Name}\",");
                    WriteLine($".finalizer = {finalizerId}");

                    Unindent();
                    WriteLine("};");
                }
                PopBlock(NewLineKind.BeforeNextBlock);

                PushBlock();
                {
                    WriteLine($"static JSCFunctionListEntry funcDef_{GetCIdentifier(Context, @class)}[]");
                    WriteOpenBraceAndIndent();

                    var funcGen = new QuickJSClassFuncDef(Context);
                    funcGen.Indent(CurrentIndentation);
                    funcGen.VisitClassDeclContext(@class);
                    funcGen.GenerateToString(@class);

                    Write(funcGen.Generate());

                    Unindent();
                    WriteLine("};");
                }
                PopBlock(NewLineKind.BeforeNextBlock);
            }
            PopBlock(NewLineKind.BeforeNextBlock);

            PushBlock(BlockKind.Class, @class);
            {
                Write($"static void register_class_{GetCIdentifier(Context, @class)}");
                WriteLine("(JSContext *ctx, JSModuleDef *m, bool set, int phase)");
                WriteOpenBraceAndIndent();

                var sources = new QuickJSRegisterImpl(Context);
                sources.Indent(CurrentIndentation);
                @class.Visit(sources);
                Write(sources.Generate());

                UnindentAndWriteCloseBrace();
            }
            PopBlock(NewLineKind.BeforeNextBlock);

            return(false);
        }