Beispiel #1
0
        public override void Process()
        {
            WriteInclude("node/node_api.h", CInclude.IncludeKind.Angled);
            WriteInclude("NAPIHelpers.h", CInclude.IncludeKind.Quoted);
            NewLine();

            PushBlock();
            foreach (var unit in TranslationUnits)
            {
                var name = NAPISources.GetTranslationUnitName(unit);
                WriteLine($"extern void register_{name}(napi_env env, napi_value exports);");
            }
            PopBlock(NewLineKind.BeforeNextBlock);

            PushBlock();
            WriteLine("// napi_value NAPI_MODULE_INITIALIZER(napi_env env, napi_value exports)");
            WriteLine("NAPI_MODULE_INIT()");
            WriteOpenBraceAndIndent();

            foreach (var unit in TranslationUnits)
            {
                var name = NAPISources.GetTranslationUnitName(unit);
                WriteLine($"register_{name}(env, exports);");
            }
            NewLine();

            WriteLine("return nullptr;");

            UnindentAndWriteCloseBrace();
            PopBlock(NewLineKind.BeforeNextBlock);
        }
Beispiel #2
0
        public override void Process()
        {
            GenerateFilePreamble(CommentKind.BCPL);
            NewLine();

            PushBlock(BlockKind.Includes);
            {
                WriteInclude("CppSharp_QuickJS.h", CInclude.IncludeKind.Angled);

                foreach (var unit in TranslationUnits)
                {
                    WriteInclude(GetIncludeFileName(Context, unit), CInclude.IncludeKind.Quoted);
                }
                NewLine();
            }
            PopBlock();
            NewLine();

            WriteLine("extern \"C\" {");
            NewLine();

            PushBlock();
            {
                foreach (var unit in TranslationUnits.Where(unit => unit.IsGenerated))
                {
                    var name = NAPISources.GetTranslationUnitName(unit);
                    WriteLine($"extern void register_{name}(JSContext *ctx, JSModuleDef *m, bool set, int phase);");
                }
            }
            PopBlock();
            NewLine();

            WriteLine("#define countof(x) (sizeof(x) / sizeof((x)[0]))");
            NewLine();

            var moduleName = module.LibraryName;

            // Generate init function.
            WriteLine($"static int js_{moduleName}_init(JSContext* ctx, JSModuleDef* m)");
            WriteOpenBraceAndIndent();

            WriteLine("for (int phase = 0; phase < 2; phase++)");
            WriteOpenBraceAndIndent();
            {
                WriteLine("register_CppSharp_QuickJS(ctx, m,  /*set=*/true, phase);");
                NewLine();

                foreach (var unit in TranslationUnits.Where(unit => unit.IsGenerated))
                {
                    var name = NAPISources.GetTranslationUnitName(unit);
                    WriteLine($"register_{name}(ctx, m, /*set=*/true, phase);");
                }
            }
            UnindentAndWriteCloseBrace();
            NewLine();

            WriteLine("return 0;");
            UnindentAndWriteCloseBrace();
            NewLine();

            // Generate module initializer.
            WriteLine("#ifdef JS_SHARED_LIBRARY");
            WriteLine("#define JS_INIT_MODULE js_init_module");
            WriteLine("#else");
            WriteLine($"#define JS_INIT_MODULE js_init_module_{moduleName}");
            WriteLine("#endif");
            NewLine();

            Write("extern \"C\" ");
            WriteLine("JSModuleDef *JS_INIT_MODULE(JSContext *ctx, const char *module_name)");
            WriteOpenBraceAndIndent();

            WriteLine("JSModuleDef* m;");
            WriteLine($"m = JS_NewCModule(ctx, module_name, js_{moduleName}_init);");
            WriteLine("if (!m)");
            WriteLineIndent("return nullptr;");
            NewLine();

            WriteLine("register_CppSharp_QuickJS(ctx, m,  /*set=*/false, 0);");
            NewLine();

            foreach (var unit in TranslationUnits)
            {
                var name = NAPISources.GetTranslationUnitName(unit);
                WriteLine($"register_{name}(ctx, m, /*set=*/false, 0);");
            }
            NewLine();

            WriteLine("return m;");

            UnindentAndWriteCloseBrace();

            NewLine();
            WriteLine("}");
        }