nested-structs/arrays (C)

未關閉
#8 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

評估

難度
4/5
預估耗時
3-5 天
新手友好度
28/100
Issue 類型
功能
描述清晰度
需要釐清
活躍度
停滯
技術堆疊
c, javascript
領域
tooling

研究方向

從 tools/gen_lib.js 中的 struct 生成迴圈附近開始,檢查 api.json 的 struct 欄位,尤其是目前被跳過的陣列型別和巢狀 struct 型別。先檢查 issue #9,然後查看產生的 src/structs.h 和 src/lib.c;完成的標準是這些欄位不再從產生的公用程式中被靜默省略。

由索引模型根據 Issue 內容生成。

描述

C enhancement todo

I am currently skipping these in C codegen. They should be addressed, once I figure out #9

https://github.com/konsumer/raylib-pointers/blob/f365325/tools/gen_lib.js#L22


// this will generate raylib-pointers

import { writeFile } from 'fs/promises'
import api from './api.json'

const { structs, wrapped } = api

let out = []

// build utils for structs
for (const { name, description, fields } of structs) {
  out.push(`// ${description}

unsigned int ${name}_size() {
  return sizeof(${name});
}`)
  for (const field of fields) {
    if (field.type.includes('[')) {
      // TODO: add utils for arrays
    } else if (/^[A-Z]/.test(field.type)) {
      // cheap way to detect raylib types
      // TODO: add utils for nested structs
    } else {
      out.push(`${field.type} ${name}_get_${field.name}(${name}* p) {\n  return p->${field.name};\n}`)
      out.push(`void ${name}_set_${field.name}(${name}* p, ${field.type} v) {\n  p->${field.name} = v;\n}`)
    }
  }
  out.push('')
}

await writeFile('src/structs.h', out.join('\n'))

out = [
  `#include <stdlib.h>
#include "raylib.h"

// utils for operating on structs
#include "./structs.h"

// this allows us to free anything that was allocated, last-frame
// max number of allocations you can do, per-frame
#define FRAME_ALLOC_SIZE 1024*512

int _frame_alloc_counter = 0;
void* _frame_allocated[FRAME_ALLOC_SIZE] = {};
bool running = false;

void* wrapped_alloc(size_t s) {
  void* p = RL_MALLOC(s);
  if (running) {
    _frame_allocated[_frame_alloc_counter] = p;
    _frame_alloc_counter = (_frame_alloc_counter + 1) % FRAME_ALLOC_SIZE;
  }
  return p;
}

void wrapped_free(void* p) {
  RL_FREE(p);
}

bool wrapped_WindowShouldClose() {
  running = true;
  for (int i=0;i<_frame_alloc_counter;i++) {
    wrapped_free(_frame_allocated[i]);
  }
  _frame_alloc_counter = 0;
  return WindowShouldClose();
}
`
]

// these will not be ghenerated for
const blocklist = [
  // I will be implementing my own mem-management stuff
  'wrapped_WindowShouldClose',
  'wrapped_free',
  'wrapped_alloc'
]

for (const { name, description, returnType, params, original } of wrapped) {
  if (blocklist.includes(name)) {
    continue
  }
  // does return need to be converted to a pointer?
  const returnPointer = /^[A-Z]/.test(returnType)

  // do any params need to be converted to pointers?
  let paramPointer = false

  const inputParams = params.map((p) => {
    if (/^[A-Z]/.test(p.type)) {
      paramPointer = true
      return `${p.type.trim()} ${p.name}`
    } else {
      return `${p.type} ${p.name}`
    }
  })

  const callParams = params.map((p) => {
    if (/^[A-Z]/.test(p.type)) {
      paramPointer = true
      return `*${p.name}`
    } else {
      return p.name
    }
  })

  out.push(`// ${description}`)
  if (returnPointer) {
    out.push(`${returnType} ${name}(${inputParams.join(', ')}) {`)
    out.push(`  ${returnType} _ret = wrapped_alloc(sizeof(${returnType.replace('*', '')}));`)
    out.push(`  *_ret = ${original}(${callParams.join(', ')});`)
    out.push('  return _ret;')
  } else {
    out.push(`${returnType} ${name}(${inputParams.join(', ')}) {`)
    if (returnType === 'void') {
      out.push(`  ${original}(${callParams.join(', ')});`)
    } else {
      out.push(`  return ${original}(${callParams.join(', ')});`)
    }
  }
  out.push('}\n')
}

await writeFile('src/lib.c', out.join('\n'))

主要語言
C
星號
0
分支
1
PR 合併指標
30 天內沒有已合併 PR

環境準備

這個專案沒有提供開發容器、Dockerfile 或貢獻指南,環境需要你自己搭建:先看它的 README,通用步驟見我們的新手貢獻指南。

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

konsumer/raylib-pointers 的其他 Issue

查看 konsumer/raylib-pointers 的全部 Issue

相似的 Issue

更多 C Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。