Subj : Re: String property & DOM synchro To : netscape.public.mozilla.jseng From : Brendan Eich Date : Tue Jun 29 2004 09:36 am Lorenzo Pastrana wrote: > case STR: > // This works for simple Get/Set but on > // obj.strprop.concat(otherstr); it fails to update the DOM > return STRING_TO_JSVAL( > jsNewString(jscontext, > (char*)((str&)_data).Export(), > ((str&)_data).GetLength())); I'm assuming Export makes a copy? Might want to check for null if out-of-memory is possible on your OS. >>You may be misusing the JS_NewString API. > > > It might be, but anyway this is not a memory management problem really : > > - I want to rely on SM's JSString 'sandard' implementation in JS space > - I dont own JSString code and I'm not inclined to branch Good, and good -- I wouldn't have it any other way ;-). > - My DOM engine is accessed by messages, so the buffer I provide to > JS_NewString is allready a copy. Right, Export() above. > - The string implementation I'm using in the background does not want > to 'share' it's buffer (usual encapsulation/ownership problem).. That means you have to copy, but it doesn't say anything about synchronizing string values between the two string-spaces. > - Since the new JSString works on a copy of the data, it needs to be > re-synched into the DOM after modifications It's not the string (JSString) that needs to be changed (JSStrings should not in general be modified -- there could be several references to one, and you can't edit the chars stored in it safely unless you know you have the only reference). Rather, the property is what gets modified. So why can't your generic_setter handle that? > - At this point I'm missing some notification mechanism called by > JSString or whatever other solution :-/ You have the wrong level of data structure. A string is a value, like 3.14 or true. There's no reference type (pay no attention to the C pointer behind the curtain! :-), no notification for changes to the immutable value (the Unicode character sequence), and indeed no mutation methods explicitly provided for JSString. The value container that can be mutated, and that does have well-defined notification APIs, is the object property. So you want JSClass.setProperty or a per-property setter. /be .