Compatibility with the typeof, instanceof keywords

Is there any expected support for the mentioned keywords in the future? Currently it's not such a big deal when working with typeof as it can be easily polyfilled with inlining. While speaking of instanceof, I haven't seemed to find any intrinsics in the API (see below), which would allow me to pass the wanted types. I know it's not doable with just simple __asm__ as it's quite limited for this case
instanceof<client::Object>(object);
instanceof<client::Object>(object);
9 Replies
apignotti
apignotti8mo ago
(Replacing a previous answer, since I did not fully understood the problem at first). Can you elaborate on what are the limitations you see with using __asm__? That would help in understanding the exact scope of your requirements. Extending C++ in terms of new keyword is not something that we would consider, but the compiler could be made powerful enough to express what you want as a normal template method.
Awesome
AwesomeOP8mo ago
Sure, I wasn't exactly talking about extending it with a new keyword as it would likely break the compatibility with C++ but more of an adding a support at a compiler intrinsics level, which would allow the types to be passed as actual values via the template, the asm, as I said before is quite limited in regards to passing those
__asm__("%1 instanceof %2" :"=r"(result): "r"(object), "r"(client::Array)); // in C++ types are not allowed to be passed as runtime values, resulting in an invalid code
__asm__("%1 instanceof %2" :"=r"(result): "r"(object), "r"(client::Array)); // in C++ types are not allowed to be passed as runtime values, resulting in an invalid code
apignotti
apignotti8mo ago
@Chen Do you see any reasonable way to provide support for something along those lines? I'd rather avoid something completely ad-hoc Possible suggestion: ->instanceOf<T>()
DutChen18
DutChen188mo ago
This seems possible, either by adding a special static variable to all client types that stores the corresponding javascript type, or with an templated intrinsic that returns the javascript type for a client type. It should then be relatively simple to implement functions like typeof and instanceof in c++.
Awesome
AwesomeOP8mo ago
I'm not sure if having it as a part of an object would be a good idea, mixing JS object's methods with Cheerp's ones feels a bit questionable Personally I feel like leaving for it a dedicated templated intrinsic would be pretty acceptable, like for example client::instanceof<T>()/cheerp::instanceof<T>()?
DutChen18
DutChen188mo ago
After some more discussion we have decided that we want to implement this, though it will likely be implemented as a member of the object to stay consistent with ->cast(). There is some other work I need to finish first before I can start on this, but if you're interested I can let you know when it's done.
Awesome
AwesomeOP8mo ago
Okay, got it, yes that's fine, good to know, it would be nice to have it as it comes in pretty handy when dealing with weakly typed parameters. Besides that I'll probably wait till the next stable release anyway.
apignotti
apignotti8mo ago
@Awesome Marking the question as resolved, work on the requested feature has been allocated and will start soon
DutChen18
DutChen187mo ago
instanceof and _typeof are now available in nightly.
object->instanceof<client::String>();
object->_typeof();
object->instanceof<client::String>();
object->_typeof();
Want results from more Discord servers?
Add your server