Skip to content

Commit 542f549

Browse files
KANIN KEARPIMYKANIN KEARPIMY
authored andcommitted
directly emit code
1 parent c83e239 commit 542f549

File tree

3 files changed

+18
-27
lines changed

3 files changed

+18
-27
lines changed

spy/backend/c/cbackend.py

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -190,30 +190,6 @@ def cwrite(self) -> None:
190190
print(f"---- {c_mod.cfile} ----")
191191
print(highlight_src("C", c_mod.cfile.read())) # type: ignore
192192

193-
argv_cfile = self._maybe_write_argv_helper()
194-
if argv_cfile is not None:
195-
self.cfiles.append(argv_cfile)
196-
197-
def _maybe_write_argv_helper(self) -> Optional[py.path.local]:
198-
from spy.fqn import FQN
199-
200-
fqn_main = FQN([self.main_modname, "main"])
201-
if fqn_main not in self.vm.globals_w:
202-
return None
203-
w_main = self.vm.globals_w[fqn_main]
204-
if not isinstance(w_main, W_ASTFunc):
205-
return None
206-
if len(w_main.w_functype.params) != 1:
207-
return None
208-
209-
# spy_wrap_argv depends on the generated _list.c and _list.h for the
210-
# list[str] layout, so it can't be precompiled into libspy. Instead we
211-
# copy list.c into the build directory and prepend the generated header.
212-
list_c = spy.libspy.SRC.join("list.c")
213-
cfile = self.build_dir.join("src", "list.c")
214-
cfile.write_binary(b'#include "spy_structdefs.h"\n' + list_c.read_binary())
215-
return cfile
216-
217193
def write_build_script(self) -> None:
218194
assert self.cfiles != [], "call .cwrite() first"
219195
wasm_exports = []

spy/backend/c/cmodwriter.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,24 @@ def init_c(self) -> None:
161161
returns_i32 = w_main.w_functype.w_restype == B.w_i32
162162

163163
if needs_argv:
164-
self.tbh_includes.wl('#include "spy/list.h"')
164+
self.tbh.wb("""
165+
#define spy_list_str spy__list$list__builtins$str$_ListImpl
166+
#define spy_list_str_new spy__list$list__builtins$str$_ListImpl$__new__
167+
#define spy_list_str_push spy__list$list__builtins$str$_ListImpl$_push
168+
169+
spy_list_str
170+
spy_wrap_argv(int argc, const char *argv[]) {
171+
spy_list_str lst = spy_list_str_new();
172+
for (int i = 0; i < argc; i++) {
173+
size_t size_str = strlen(argv[i]);
174+
spy_Str *allo = spy_str_alloc(size_str);
175+
char *buf = (char *)allo->utf8;
176+
memcpy(buf, argv[i], size_str);
177+
lst = spy_list_str_push(lst, allo);
178+
}
179+
return lst;
180+
}
181+
""")
165182

166183
if needs_argv and returns_i32:
167184
execution_code = f"""

spy/tests/test_cli.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,6 @@ def main(argv: list[str]) -> None:
293293
hsrc = main_h.read()
294294
assert "spy_wrap_argv" in csrc
295295
assert "int main(int argc" in csrc
296-
assert "spy/list.h" in hsrc
297296

298297
def test_argv_with_exit_code_cwrite(self):
299298
src = """
@@ -310,7 +309,6 @@ def main(argv: list[str]) -> i32:
310309
hsrc = main_h.read()
311310
assert "spy_wrap_argv" in csrc
312311
assert "int main(int argc" in csrc
313-
assert "spy/list.h" in hsrc
314312

315313
def test_no_argv_no_wrap_cwrite(self):
316314
src = """

0 commit comments

Comments
 (0)