示例#1
0
        public override bool VisitEnumDecl(Enumeration @enum)
        {
            if (@enum.IsIncomplete)
            {
                return(false);
            }

            PushBlock(BlockKind.Enum);
            {
                Write($"static void register_enum_{GetCIdentifier(Context, @enum)}");
                WriteLine("(JSContext *ctx, JSModuleDef *m, bool set)");
                WriteOpenBraceAndIndent();

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

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

                WriteLine($"int status = JS_SetModuleExport(ctx, m, \"{@enum.Name}\", val);");
                WriteLine("assert(status != -1);");

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

            return(true);
        }
示例#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);
        }