JSON prebuild export and import broken?
- Vorherrschende Sprache
- JavaScript
- Sterne
- 15.5k
- Forks
- 663
- PR-Merge-Kennzahlen
- Keine gemergten PRs in 30 T.
Beschreibung
## *What* is wrong?
To make the loading time faster, I thought to prebuild my kernels once as JSON with toJSON(), and use this JSON to create my MapKernels. But the toJSON() is out of date (not handling native functions correct), and also the intern fromJSON(...) do not work. I tried to patch them, and now I can export, but I don't get the import working.
Additional information: I use a map function with some map return values, and custom native functions.
## *Where* does it happen?
Not specific to a system.
## *How* do we replicate the issue?
- Create a kernel with createKernelMap(...)
- Use at least one map function
- Use at least one native function
- Export the kernel with toJSON()
- Try to create a new kernel with that JSON.
## *How* important is this (1-5)?
2
## Expected behavior (i.e. solution)
Should work
## Other Comments
After some tests to patch fromJSON(...) I got it working, but the compiled fragment shader complains about complains about the missing map result variables.
Here are my patched functions:
```
toJSON = function() {
return this.traceFunctionCalls(this.rootNode.name).reverse().map(name => {
const nativeIndex = this.nativeFunctionNames.indexOf(name);
if (nativeIndex > -1) {
return {
name,
source: this.nativeFunctions[nativeIndex].source
};
} else if (this.functionMap[name]) {
return this.functionMap[name].toJSON();
} else {
throw new Error(`function ${ name } not found`);
}
});
}
```
```
fromJSON = function(jsonFunctionNodes, FunctionNode) {
this.functionMap = {};
for (let i = 0; i < jsonFunctionNodes.length; i++) {
const jsonFunctionNode = jsonFunctionNodes[i];
if (jsonFunctionNode.settings) {
const functionNode = new FunctionNode(jsonFunctionNode.ast, jsonFunctionNode.settings);
functionNode.triggerImplyArgumentType = this.assignArgumentType.bind(this);
// For some reason the FunctionNode object is missing some members, which I found in the FunctionBuilder.
// I'm not sure, if this is correct, but it works somehow.
for (const key of Object.keys(functionNode)) {
if (functionNode[key] === null && this[key] && typeof this[key] === 'function') {
functionNode[key] = this[key].bind(this);
}
}
this.addFunctionNode(functionNode);
if (functionNode.isSubKernel) {
this.subKernelNodes.push(functionNode);
}
this.functionNodes.push(functionNode);
} else {
this.nativeFunctions.push(jsonFunctionNode);
}
}
return this;
};
```
Beitragsleitfaden
Rechercherichtung
Beginne mit createKernelMap und den Einstiegspunkten toJSON/fromJSON und untersuche anschließend die Behandlung von FunctionNode für Map-Funktionen und native Funktionen. Reproduziere die im Issue beschriebene Export-/Import-Sequenz und verfolge die Ausgabe des kompilierten Fragment Shaders. Als abgeschlossen gilt die Aufgabe, wenn ein Kernel mit Map-Rückgabewerten und benutzerdefinierten nativen Funktionen exportiert, importiert und kompiliert werden kann, ohne dass Map-Ergebnisvariablen fehlen.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- javascript
- Bereich
- compilers
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 35/100