From 820c1324dcebcf35f7d5886af5967089b8e487db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thiago=20Fran=C3=A7a=20da=20Silva?= Date: Mon, 14 Feb 2022 22:30:32 -0300 Subject: [PATCH] WebIDL: Use malloc/strcpy on Struct String setters, fix #77 From @TheOneric: For most libass APIs the char* strings remain owned by the caller with libass doing internal copies if needed. This means the caller must if necessary free the memory at some point after the libass API call finished. The track and event modification/creation APIs are different because they work close to library internals (which is also why their usage comes with more restrictions/obligations than other APIs and why they don't sensibly work with ass.h alone). Since we transfer ownership of the pointer to libass, this is unlikely to cause memory leaks. --- build/webidl_binder.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/build/webidl_binder.py b/build/webidl_binder.py index faedf10c..a8218e71 100644 --- a/build/webidl_binder.py +++ b/build/webidl_binder.py @@ -2,6 +2,7 @@ ## Patched to: ## - add integer pointers (IntPtr) ## - implement ByteString type +## - fix string setter for structs ## From https://github.com/emscripten-core/emscripten/blob/f36f9fcaf83db93e6a6d0f0cdc47ab6379ade139/tools/webidl_binder.py # Copyright 2014 The Emscripten Authors. All rights reserved. @@ -723,7 +724,12 @@ def render_function(class_name, func_name, sigs, return_type, non_pointer, get_sigs = {0: []} set_sigs = {1: [Dummy({'type': m.type})]} get_call_content = take_addr_if_nonpointer(m) + 'self->' + attr - set_call_content = 'self->' + attr + ' = ' + deref_if_nonpointer(m) + 'arg0' + if m.type.isDOMString(): + set_call_content = """char* copy = (char*)malloc(strlen(%s) + 1); + strcpy(copy, %s); + self->%s = copy""" % (deref_if_nonpointer(m) + 'arg0', deref_if_nonpointer(m) + 'arg0', attr) + else: + set_call_content = 'self->' + attr + ' = ' + deref_if_nonpointer(m) + 'arg0' get_name = 'get_' + attr mid_js += [r'''