Patch to make Javascript writeln() work

Dave Higton dave at davehigton.me.uk
Thu Sep 24 21:02:09 BST 2015


In message <20150924082904.GA14703 at somnambulist.local>
          Daniel Silverstone <dsilvers at netsurf-browser.org> wrote:

>On Wed, Sep 23, 2015 at 21:43:13 +0100, Dave Higton wrote:
>> Second version using SLEN() instead of strlen().
>> 
>> This patch to Document/bnd makes the Javascript writeln() function
>> work as well as write().
>
>>From my reading of the spec, it inserts a newline, not a line break.

I agree!  I was playing with some test code today that made me realise
the the results of writeln() are very different if enclosed within
<pre></pre> tags.  When I submit a test method, I'll use that.

>Also, you could chain through Document.write to reduce code duplication
>
>(untested)
>
>method Document::writeln()
>%{
>	duk_push_this(ctx);
>	duk_insert(ctx, 0);
>	duk_push_lstring(ctx, "write", SLEN("write"));
>	duk_insert(ctx, 1);
>	duk_push_lstring(ctx, "\n", SLEN("\n"));
>	duk_call_prop(ctx, 0, duk_get_top(ctx) - 1);
>	return 0;
>%}
>
>This has the advantage that if we change how Document::write functions
>then we don't need to remember to change that in writeln.  Also it gives
>us an example in the codebase of how to invoke a method on an object
>in our bindings which may be of use later.
>
>I don't object to your previous approach, just think this might be cleaner.
>
>Opinions?

If I understand correctly, this builds a call to the write() function,
although at my present level of knowledge I don't understand how it
differentiates between "write" being a call to a function and "\n"
being data that will be appended to the evaluated expression that the
function renders to screen.

You pay two small prices:

1) it's slower;

2) the log says "Writing..." regardless of whether write() or writeln()
has been called.

Neither of them is in any way a show stopper of course.

Like you, I prefer to share code where possible, but I don't find this
solution very appealing.

I'd like to call a third function, passing a boolean argument to
represent whether write() or writeln() is called, and make the choice
at two places in the code.  But where do you put the third function,
which looks like the Document::write() method but doesn't map to any
Javascript method?

Dave

____________________________________________________________
FREE ONLINE PHOTOSHARING - Share your photos online with your friends and family!
Visit http://www.inbox.com/photosharing to find out more!



More information about the netsurf-dev mailing list