-
Notifications
You must be signed in to change notification settings - Fork 156
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
external objects are not really JS objects #16
Comments
Hi @xeioex! var logger;
function helper(args) {
logger('some message');
}
function handler(req, res) {
logger = req.error.bind(req);
...
} Is it no-go, and i should use a closure here? |
Yes, currently it is not possible. Using closures can be a way. |
Partly fixed within #20. |
Do I understand it correctly that this issue makes things like JSON.stringify() not work for the objects constructed in externally in C-code (like the request object, for example)? The issue is currently labeled as "in progress". Is it in progress? What's the reasonable timeline for the fix here? Is it weeks, months? Thanks! |
@drsm thanks! Ok, so JSON.stringify() is supported now. The second part of the question remains. Do you guys plan to make the external object indistinguishable from the native JS objects as per this issue? Is the in-progress state accurate, and if yes, when do you think it may be delivered? I'm not asking you to commit to any dates, of course, but any kind of heads up on the timeline is much appreciated. |
Yes, we do.
currently it is on pause. I plan to do this in 3-month timeframe. |
Take a look at the following patch (beta): https://gist.github.com/112de4cc2de34a9dbdccbc6bbbcd23c3 Now external definitions are converted to native JS object primitives. External queries like in r.headersOut are implemented using mechanism similar to Definition example: {
.flags = NJS_EXTERN_OBJECT,
.name.string = njs_str("headersOut"), // may also be a symbol
.writable = 1,
.configurable = 1,
.enumerable = 1,
.u.object = {
// may implement [[Get]] (and [[Set]], [[Delete]] if writable or configurable is true)
.prop_handler = ngx_http_js_ext_header_out, // njs_prop_handler_t
.keys = ngx_http_js_ext_keys_header_out,
}
},
Result >> console.toString()
'[object Console]'
>> console[0]='a'; console[1]='b'; console.length =2; console.__proto__ = Array.prototype; console.join('|')
'a|b'
>> Object.getOwnPropertyNames(console)
[
'log',
'0',
'1',
'length',
'dump',
'time',
'timeEnd'
]
>> JSON.stringify(console)
'{"0":"a","1":"b","length":2}'
>> console
Console {
log: [Function: native],
0: 'a',
1: 'b',
length: 2,
dump: [Function: native],
time: [Function: native],
timeEnd: [Function: native]
} |
I'm wondering when you plan to commit? I need to deep into code again, it changes a lot.
|
By the end of this week.
zero. Because our external objects are not covered by test262.
Short term plan includes:
nginx.conf:
|
@xeioex
Others look good.
BTW, how do you get |
Does it mean It's common that we need to do the same things on many servers. Such as authentication. Is anyone on it? If not, I'd like to try this. |
agree.
https://tc39.es/ecma262/#sec-object-internal-methods-and-internal-slots In the spec, the slots correspond to In my definition [[Get]], [[Set]], [[Delete]] are merged to a single typedef struct {
//...
njs_prop_handler_t prop_handler;
unsigned writable:1;
unsigned configurable:1;
unsigned enumerable:1;
njs_exotic_keys_t keys;
// will be more later for other types
} njs_exotic_slots_t; /* ..
* TODO:
* Object.defineProperty([1,2], '1', {configurable:false})
*/
njs_int_t
njs_property_query(njs_vm_t *vm, njs_property_query_t *pq, njs_value_t *value,
njs_value_t *key)
{ Good catch, the issue already fixed in 58ebb38. The comment will be removed.
I had some general idea and refined it in the process of development. |
most probably yes, but far into the future.
yes. that is why splitting your logic into different modules helps. Now, you have to merge all of your code, probably for different, unrelated locations into one js_include file.
not yet, I plan to do this after this issue. |
njs 0.4.0 is released: http://nginx.org/en/docs/njs/changes.html#njs0.4.0 |
Currently objects created externally (
request
from nginx module, for example) are implemented in a completely different way from native JS objects (NJS_EXTERNAL
value type). It means that in every JS function a special branch is needed to support externals.Instead external objects should be implemented using generic primitives used for native JS objects.
As a result external objects will be indistinguishable from native objects and will be supported by all JS functions.
The text was updated successfully, but these errors were encountered: