Skip to content

Commit f7c1f35

Browse files
committed
move context to compile() and remove unneded params floating around
1 parent f1b7d32 commit f7c1f35

File tree

2 files changed

+61
-112
lines changed

2 files changed

+61
-112
lines changed

packages/cubejs-schema-compiler/src/compiler/DataSchemaCompiler.ts

Lines changed: 58 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ export class DataSchemaCompiler {
140140

141141
private readonly compiledScriptCache: LRUCache<string, vm.Script>;
142142

143-
private compileV8ContextCache: vm.Context;
143+
private compileV8ContextCache: vm.Context | null = null;
144144

145145
// FIXME: Is public only because of tests, should be private
146146
public compilePromise: any;
@@ -174,7 +174,6 @@ export class DataSchemaCompiler {
174174
this.workerPool = null;
175175
this.compilerId = options.compilerId || 'default';
176176
this.compiledScriptCache = options.compiledScriptCache;
177-
this.compileV8ContextCache = vm.createContext({});
178177
}
179178

180179
public compileObjects(compileServices, objects, errorsReport: ErrorReporter) {
@@ -308,8 +307,60 @@ export class DataSchemaCompiler {
308307
let compiledFiles: Record<string, boolean> = {};
309308
let asyncModules: CallableFunction[] = [];
310309

310+
this.compileV8ContextCache = vm.createContext({
311+
view: (name, cube) => (
312+
!cube ?
313+
this.cubeFactory({ ...name, fileName: file.fileName, isView: true }) :
314+
cubes.push({ ...cube, name, fileName: file.fileName, isView: true })
315+
),
316+
cube: (name, cube) => (
317+
!cube ?
318+
this.cubeFactory({ ...name, fileName: file.fileName }) :
319+
cubes.push({ ...cube, name, fileName: file.fileName })
320+
),
321+
context: (name, context) => contexts.push({ ...context, name, fileName: file.fileName }),
322+
addExport: (obj) => {
323+
exports[file.fileName] = exports[file.fileName] || {};
324+
exports[file.fileName] = Object.assign(exports[file.fileName], obj);
325+
},
326+
setExport: (obj) => {
327+
exports[file.fileName] = obj;
328+
},
329+
asyncModule: (fn) => {
330+
asyncModules.push(fn);
331+
},
332+
require: (extensionName) => {
333+
if (this.extensions[extensionName]) {
334+
return new (this.extensions[extensionName])(this.cubeFactory, this, cubes);
335+
} else {
336+
const foundFile = this.resolveModuleFile(file, extensionName, toCompile, errorsReport);
337+
if (!foundFile && this.allowNodeRequire) {
338+
if (extensionName.indexOf('.') === 0) {
339+
extensionName = path.resolve(this.repository.localPath(), extensionName);
340+
}
341+
// eslint-disable-next-line global-require,import/no-dynamic-require
342+
const Extension = require(extensionName);
343+
if (Object.getPrototypeOf(Extension).name === 'AbstractExtension') {
344+
return new Extension(this.cubeFactory, this, cubes);
345+
}
346+
return Extension;
347+
}
348+
this.compileFile(
349+
foundFile,
350+
errorsReport,
351+
compiledFiles,
352+
[],
353+
{ doSyntaxCheck: true }
354+
);
355+
exports[foundFile.fileName] = exports[foundFile.fileName] || {};
356+
return exports[foundFile.fileName];
357+
}
358+
},
359+
COMPILE_CONTEXT: this.standalone ? this.standaloneCompileContextProxy() : this.cloneCompileContextWithGetterAlias(this.compileContext || {}),
360+
});
361+
311362
const compilePhase = async (compilers: CompileCubeFilesCompilers, stage: 0 | 1 | 2 | 3) => {
312-
const res = this.compileCubeFiles(cubes, exports, contexts, compiledFiles, asyncModules, compilers, await transpile(stage), errorsReport);
363+
const res = this.compileCubeFiles(cubes, contexts, compiledFiles, asyncModules, compilers, await transpile(stage), errorsReport);
313364

314365
// clear the objects for the next phase
315366
cubes = [];
@@ -356,6 +407,7 @@ export class DataSchemaCompiler {
356407
this.throwIfAnyErrors();
357408
}
358409
// Free unneeded resources
410+
this.compileV8ContextCache = null;
359411
this.cubeDictionary.free();
360412
this.cubeSymbols.free();
361413
return res;
@@ -527,7 +579,6 @@ export class DataSchemaCompiler {
527579

528580
private async compileCubeFiles(
529581
cubes: CubeDefinition[],
530-
exports: Record<string, Record<string, any>>,
531582
contexts: Record<string, any>[],
532583
compiledFiles: Record<string, boolean>,
533584
asyncModules: CallableFunction[],
@@ -540,10 +591,6 @@ export class DataSchemaCompiler {
540591
this.compileFile(
541592
file,
542593
errorsReport,
543-
cubes,
544-
exports,
545-
contexts,
546-
toCompile,
547594
compiledFiles,
548595
asyncModules
549596
);
@@ -560,10 +607,6 @@ export class DataSchemaCompiler {
560607
private compileFile(
561608
file: FileContent,
562609
errorsReport: ErrorReporter,
563-
cubes: CubeDefinition[],
564-
exports: Record<string, Record<string, any>>,
565-
contexts: Record<string, any>[],
566-
toCompile: FileContent[],
567610
compiledFiles: Record<string, boolean>,
568611
asyncModules: CallableFunction[],
569612
{ doSyntaxCheck } = { doSyntaxCheck: false }
@@ -575,7 +618,7 @@ export class DataSchemaCompiler {
575618
compiledFiles[file.fileName] = true;
576619

577620
if (file.fileName.endsWith('.js')) {
578-
this.compileJsFile(file, errorsReport, cubes, contexts, exports, asyncModules, toCompile, compiledFiles, { doSyntaxCheck });
621+
this.compileJsFile(file, errorsReport, { doSyntaxCheck });
579622
} else if (file.fileName.endsWith('.yml.jinja') || file.fileName.endsWith('.yaml.jinja') ||
580623
(
581624
file.fileName.endsWith('.yml') || file.fileName.endsWith('.yaml')
@@ -585,17 +628,11 @@ export class DataSchemaCompiler {
585628
asyncModules.push(() => this.yamlCompiler.compileYamlWithJinjaFile(
586629
file,
587630
errorsReport,
588-
cubes,
589-
contexts,
590-
exports,
591-
asyncModules,
592-
toCompile,
593-
compiledFiles,
594631
this.standalone ? {} : this.cloneCompileContextWithGetterAlias(this.compileContext),
595632
this.pythonContext!
596633
));
597634
} else if (file.fileName.endsWith('.yml') || file.fileName.endsWith('.yaml')) {
598-
this.yamlCompiler.compileYamlFile(file, errorsReport, cubes, contexts, exports, asyncModules, toCompile, compiledFiles);
635+
this.yamlCompiler.compileYamlFile(file, errorsReport);
599636
}
600637
}
601638

@@ -627,12 +664,6 @@ export class DataSchemaCompiler {
627664
public compileJsFile(
628665
file: FileContent,
629666
errorsReport: ErrorReporter,
630-
cubes: CubeDefinition[],
631-
contexts: Record<string, any>[],
632-
exports: Record<string, Record<string, any>>,
633-
asyncModules: CallableFunction[],
634-
toCompile: FileContent[],
635-
compiledFiles: Record<string, boolean>,
636667
{ doSyntaxCheck } = { doSyntaxCheck: false }
637668
) {
638669
if (doSyntaxCheck) {
@@ -647,70 +678,9 @@ export class DataSchemaCompiler {
647678

648679
try {
649680
const script = this.getJsScript(file);
650-
651-
const sandboxLocals = Object.freeze({
652-
view: (name, cube) => (
653-
!cube ?
654-
this.cubeFactory({ ...name, fileName: file.fileName, isView: true }) :
655-
cubes.push({ ...cube, name, fileName: file.fileName, isView: true })
656-
),
657-
cube: (name, cube) => (
658-
!cube ?
659-
this.cubeFactory({ ...name, fileName: file.fileName }) :
660-
cubes.push({ ...cube, name, fileName: file.fileName })
661-
),
662-
context: (name, context) => contexts.push({ ...context, name, fileName: file.fileName }),
663-
addExport: (obj) => {
664-
exports[file.fileName] = exports[file.fileName] || {};
665-
exports[file.fileName] = Object.assign(exports[file.fileName], obj);
666-
},
667-
setExport: (obj) => {
668-
exports[file.fileName] = obj;
669-
},
670-
asyncModule: (fn) => {
671-
asyncModules.push(fn);
672-
},
673-
require: (extensionName) => {
674-
if (this.extensions[extensionName]) {
675-
return new (this.extensions[extensionName])(this.cubeFactory, this, cubes);
676-
} else {
677-
const foundFile = this.resolveModuleFile(file, extensionName, toCompile, errorsReport);
678-
if (!foundFile && this.allowNodeRequire) {
679-
if (extensionName.indexOf('.') === 0) {
680-
extensionName = path.resolve(this.repository.localPath(), extensionName);
681-
}
682-
// eslint-disable-next-line global-require,import/no-dynamic-require
683-
const Extension = require(extensionName);
684-
if (Object.getPrototypeOf(Extension).name === 'AbstractExtension') {
685-
return new Extension(this.cubeFactory, this, cubes);
686-
}
687-
return Extension;
688-
}
689-
this.compileFile(
690-
foundFile,
691-
errorsReport,
692-
cubes,
693-
exports,
694-
contexts,
695-
toCompile,
696-
compiledFiles,
697-
[],
698-
{ doSyntaxCheck: true }
699-
);
700-
exports[foundFile.fileName] = exports[foundFile.fileName] || {};
701-
return exports[foundFile.fileName];
702-
}
703-
},
704-
COMPILE_CONTEXT: this.standalone ? this.standaloneCompileContextProxy() : this.cloneCompileContextWithGetterAlias(this.compileContext || {}),
705-
});
706-
707-
this.compileV8ContextCache.sandboxLocals = sandboxLocals;
708-
709-
script.runInContext(this.compileV8ContextCache, { timeout: 15000 });
681+
script.runInContext(this.compileV8ContextCache!, { timeout: 15000 });
710682
} catch (e) {
711683
errorsReport.error(e);
712-
} finally {
713-
delete this.compileV8ContextCache.sandboxLocals;
714684
}
715685
}
716686

packages/cubejs-schema-compiler/src/compiler/YamlCompiler.ts

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -66,38 +66,17 @@ export class YamlCompiler {
6666
public async compileYamlWithJinjaFile(
6767
file: FileContent,
6868
errorsReport: ErrorReporter,
69-
cubes: CubeDefinition[],
70-
contexts: Record<string, any>[],
71-
exports: Record<string, Record<string, any>>,
72-
asyncModules: CallableFunction[],
73-
toCompile: FileContent[],
74-
compiledFiles: Record<string, boolean>,
7569
compileContext,
7670
pythonContext: PythonCtx
7771
) {
7872
const compiledFile = await this.renderTemplate(file, compileContext, pythonContext);
7973

80-
return this.compileYamlFile(
81-
compiledFile,
82-
errorsReport,
83-
cubes,
84-
contexts,
85-
exports,
86-
asyncModules,
87-
toCompile,
88-
compiledFiles
89-
);
74+
return this.compileYamlFile(compiledFile, errorsReport);
9075
}
9176

9277
public compileYamlFile(
9378
file: FileContent,
9479
errorsReport: ErrorReporter,
95-
cubes: CubeDefinition[],
96-
contexts: Record<string, any>[],
97-
exports: Record<string, Record<string, any>>,
98-
asyncModules: CallableFunction[],
99-
toCompile: FileContent[],
100-
compiledFiles: Record<string, boolean>
10180
) {
10281
if (!file.content.trim()) {
10382
return;
@@ -112,12 +91,12 @@ export class YamlCompiler {
11291
if (key === 'cubes') {
11392
(yamlObj.cubes || []).forEach(({ name, ...cube }) => {
11493
const transpiledFile = this.transpileAndPrepareJsFile(file, 'cube', { name, ...cube }, errorsReport);
115-
this.dataSchemaCompiler?.compileJsFile(transpiledFile, errorsReport, cubes, contexts, exports, asyncModules, toCompile, compiledFiles);
94+
this.dataSchemaCompiler?.compileJsFile(transpiledFile, errorsReport);
11695
});
11796
} else if (key === 'views') {
11897
(yamlObj.views || []).forEach(({ name, ...cube }) => {
11998
const transpiledFile = this.transpileAndPrepareJsFile(file, 'view', { name, ...cube }, errorsReport);
120-
this.dataSchemaCompiler?.compileJsFile(transpiledFile, errorsReport, cubes, contexts, exports, asyncModules, toCompile, compiledFiles);
99+
this.dataSchemaCompiler?.compileJsFile(transpiledFile, errorsReport);
121100
});
122101
} else {
123102
errorsReport.error(`Unexpected YAML key: ${key}. Only 'cubes' and 'views' are allowed here.`);

0 commit comments

Comments
 (0)