MCPcopy Create free account
hub / github.com/callstackincubator/polygen / buildLoaderSource

Function buildLoaderSource

packages/codegen/src/templates/host.ts:5–42  ·  view source on GitHub ↗
(generatedModules: W2CGeneratedModule[])

Source from the content-addressed store, hash-verified

3import { HEADER } from './common.js';
4
5export function buildLoaderSource(generatedModules: W2CGeneratedModule[]) {
6 const moduleBagType = new cpp.TypeBuilder('ModuleBag');
7
8 function makeModuleFactoryDecl(module: W2CGeneratedModule) {
9 return `std::shared_ptr<Module> ${module.moduleFactoryFunctionName}();`;
10 }
11
12 const moduleEntries = generatedModules.map((m) =>
13 cpp.exprs.initializerListOf([
14 cpp.exprs.string(m.name),
15 cpp.exprs.string(m.checksum.toString('hex')),
16 cpp.exprs.symbol(m.moduleFactoryFunctionName),
17 ])
18 );
19
20 const moduleBagVar = new cpp.VariableBuilder('moduleBag')
21 .withType(moduleBagType.asConst())
22 .withInitializer((v) => v.listOf(moduleEntries, true), true);
23
24 const moduleBagGetterFunc = new cpp.FunctionBuilder('getModuleBag')
25 .withReturnType(moduleBagVar.type.asRef())
26 .withBody((b) => b.return_(moduleBagVar.name));
27
28 const builder = new cpp.SourceFileBuilder(true);
29 builder
30 .insertRaw(HEADER)
31 .includeSystem('ReactNativePolygen/Loader.h')
32 .spacing(1)
33 .namespace('callstack::polygen::generated', (builder) =>
34 builder
35 .writeManyLines(generatedModules, makeModuleFactoryDecl)
36 .spacing(1)
37 .defineVariable(moduleBagVar)
38 .defineFunction(moduleBagGetterFunc)
39 );
40
41 return builder.toString();
42}

Callers

nothing calls this directly

Calls 15

asConstMethod · 0.95
withInitializerMethod · 0.80
withTypeMethod · 0.80
withBodyMethod · 0.80
withReturnTypeMethod · 0.80
asRefMethod · 0.80
return_Method · 0.80
namespaceMethod · 0.80
spacingMethod · 0.80
includeSystemMethod · 0.80
insertRawMethod · 0.80
defineFunctionMethod · 0.80

Tested by

no test coverage detected