Cleanup WASM bindings struct generation

PiperOrigin-RevId: 827949686
Change-Id: I4115e397f64963244e3c30a7eaff5be7534aadf2
This commit is contained in:
Matija Kecman
2025-11-04 06:43:08 -08:00
committed by Copybara-Service
parent 4b79a58fa0
commit 776fc32eb4
4 changed files with 664 additions and 497 deletions
+25 -22
View File
@@ -459,28 +459,25 @@ def _build_struct_header_internal(
builder = code_builder.CodeBuilder()
with builder.struct(f"{w}"):
if not is_mjs:
builder.line(f"{w}();")
builder.line(f"{w}(const {w} &);")
builder.line(f"{w} &operator=(const {w} &);")
builder.line(f"explicit {w}({s} *ptr);")
builder.line(f"~{w}();")
if shallow_copy:
if not is_mjs:
builder.line(f"{w}();")
if shallow_copy and not is_mjs:
builder.line(f"{w}(const {w} &);")
builder.line(f"{w} &operator=(const {w} &);")
builder.line(f"std::unique_ptr<{w}> copy();")
builder.line(f"{s}* get() const;")
builder.line(f"void set({s}* ptr);")
for field in wrapped_fields:
if field.definition and field not in fields_with_init:
for line in field.definition.splitlines():
builder.line(line)
with builder.function(f"{s}* get() const"):
builder.line("return ptr_;")
with builder.function(f"void set({s}* ptr)"):
builder.line("ptr_ = ptr;")
builder.private()
builder.line(f"{s}* ptr_;")
if not is_mjs:
@@ -491,6 +488,7 @@ def _build_struct_header_internal(
for field in fields_with_init:
if field.definition:
builder.line(f"{field.definition}")
return builder.to_string() + ";"
@@ -600,8 +598,15 @@ def build_struct_source(
with builder.function(f"{w}::{w}({s} *ptr) : ptr_(ptr){fields_init}"):
pass
# constructor with default values
# destructor
with builder.function(f"{w}::~{w}()"):
if not is_mjs:
with builder.block("if (owned_ && ptr_)"):
delete_ptr = _delete_ptr_statement(s)
builder.line(delete_ptr)
if not is_mjs:
# default constructor
with builder.function(f"{w}::{w}() : ptr_(new {s}){fields_init}"):
builder.line("owned_ = true;")
default_func = _default_function_statement(s)
@@ -626,18 +631,16 @@ def build_struct_source(
builder.line(field_with_init.ptr_copy_reset)
builder.line("return *this;")
# destructor
with builder.function(f"{w}::~{w}()"):
if not is_mjs:
with builder.block("if (owned_ && ptr_)"):
delete_ptr = _delete_ptr_statement(s)
builder.line(delete_ptr)
# copy function
if shallow_copy:
# explicit copy function
with builder.function(f"std::unique_ptr<{w}> {w}::copy()"):
builder.line(f"return std::make_unique<{w}>(*this);")
with builder.function(f"{s}* {w}::get() const"):
builder.line("return ptr_;")
with builder.function(f"void {w}::set({s}* ptr)"):
builder.line("ptr_ = ptr;")
return builder.to_string()