[Qtscript-bindings] Closure API

Kent Hansen khansen at trolltech.com
Mon Jan 12 11:12:33 CET 2009


Hi Peter,
Maybe you can use the approach shown here:
http://doc.trolltech.com/4.4/qtscript.html#nested-functions-and-the-scope-chain
QScriptValue::scope() and QScriptValue::setScope() are mistakenly
flagged as internal at the moment, but this should be fixed for 4.5.
By passing the QScriptValue::ResolveScope flag to
QScriptValue::property(), the scope will be looked up (recursively), so
that's how you implement nested closures to any depth.

Regards,
Kent


Peter Kümmel wrote:
> The current QtScript API only supports one
> closure QScriptValue per function.
> The attached file proposes an extensions to
> the API to support multiple closure values.
>
>
> In short:
>
> With the help of QScriptValue::setData()/data()
> it is possible to implement a closure on the
> C side, for instance see 1. in closure.cpp which
> implements this closure:
>
> newCountDown = function(start) {
>                  var counter = start;
>                  var countDown = function() {
>                                    counter--;
>                                    return counter;
>                                  };
>                   return countDown;
>                 };
>
>
> But if you wanna use multiple closure values
> it is necessary to:
> 1. make them fit into one QScriptValue, or
> 2. to make them properties.
>
> 1. has the disadvantage that it complicates things and
> 2. makes the closure data accessible from JS which
> definitely contradicts the idea of closures.
> Therefore the wish to extend the current QtScript API.
>
> Or is there other way to handle multiple closures which
> I have overseen?
>
> Peter
> ------------------------------------------------------------------------
>
> _______________________________________________
> Qtscript-bindings mailing list
> Qtscript-bindings at kde.org
> https://mail.kde.org/mailman/listinfo/qtscript-bindings



More information about the Qtscript-bindings mailing list