Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

JSON prebuild export and import broken?

未关闭
#599 3 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
35/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
停滞
技术栈
javascript
领域
compilers

调研方向

从 createKernelMap 和 toJSON/fromJSON 入口点开始,然后检查针对 map 函数和 native 函数的 FunctionNode 处理。复现 issue 中描述的导出/导入流程,并跟踪已编译 fragment shader 的输出。当包含 map 返回值和自定义 native 函数的 kernel 能够被导出、导入并编译,且不缺少 map 结果变量时,即表示完成。

由索引模型根据 Issue 内容生成。

描述

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;
};
主要语言
JavaScript
星标
15.5k
派生
663
PR 合并指标
30 天内没有已合并 PR

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

gpujs/gpu.js 的其他 Issue

查看 gpujs/gpu.js 的全部 Issue

相似的 Issue

更多 JavaScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。