Comments (19)
The current mechanisms for property virtualization are: (1) plain setters/getters and (2) the E6 Proxy object. Both setters/getters and Proxy traps can be native Duktape/C functions.
For more concreteness, could you describe what use case you would like to solve which is not addressed by current mechanisms?
from duktape.
If use Proxy, Does a object which is proxy break the proto chain?
from duktape.
For example,
var person = {phone:"000-000"}
var student = {age:20}
student.proto = person;
var sam = Object.create(student);
sam.name // need point about named property.
This situation, Don't describe using Proxy.
from duktape.
Hmm, just to be sure: sam
inherits from student
which inherits from person
, and name
is intended to represent a virtual property that doesn't exist in the target (sam
)?
Anyway, Proxy will capture all property reads and the 'get' trap can freely decide to provide property values out of thin air or to read them from the target object. For instance, sam.name
would invoke the 'get' trap, and you could return whatever you want for 'name' without having that property in the target object.
from duktape.
If sam Object turn Proxy, has a problem that sam object' proto is proxy.
I want that sam object's proto is student.
from duktape.
Duktape's Proxy implementation is not yet complete - do you mean that there is an issue with Duktape's Proxy or the ES6 Proxy model in general?
When you say that it's a problem that sam
object's proto is proxy, what breaks in practice? At least property reads and writes should go normally through the Proxy, so I'm not sure what concretely breaks in your code.
from duktape.
I mean we should support the general Object like V8.(http://izs.me/v8-docs/classv8_1_1ObjectTemplate.html)
from duktape.
Unfortunately that doesn't really tell me what the concrete problem is that you cannot solve with Proxy. Duktape doesn't attempt to copy V8's API so it's not really a useful feature request to copy a V8 API without concrete motivation what we're trying to achieve.
from duktape.
Just to get some concreteness, here's an example of using a Proxy to provide a virtual "name":
var person = { phone: "000-000" };
var student = { age: 20 }; Object.setPrototypeOf(student, person);
var samRaw = Object.create(student);
var sam = new Proxy(samRaw, {
get: function (targ, key) {
// Virtual properties
if (key === 'name') {
return 'fake name'; // whatever logic here
}
// Not a virtual property, continue lookup from target
return targ[key];
}
});
print('phone is:', sam.phone);
print('age is:', sam.age);
print('name is:', sam.name);
When executed with the Duktape command line, this prints:
phone is: 000-000
age is: 20
name is: fake name
Trapping property writes would be handled similarly with the set
trap.
from duktape.
@svaarala Wow. I'm understand.
print(sam.proto == student) // result : true
Thank you.
from duktape.
I'm happy if the Proxy solved your issue, but I'm not sure if it was a good solution or just an adequate solution :) There's certainly room for improvement for property / object virtualization so feel free to open an issue if you run into something that doesn't quite work :)
from duktape.
@svaarala If exist this API, I think very useful. So reopen this issue.
and Thank you for help. :)
from duktape.
@svaarala I have last question. Is it possible that wrap proxy in the global object?
from duktape.
There's a Duktape/C API call to replace the global object with a new one:
Technically you should be able to give a Proxy object as the new global object.
from duktape.
@svaarala Is this API named "duk_set_global_object" since 0.11.0?
from duktape.
The duk_set_global_object() call was added in 0.12.0.
from duktape.
@svaarala :) Thank you. 💯 Now, I'am happy. because of you.
from duktape.
Glad to help, let me know if you run into further issues :)
from duktape.
@hyunjunekim I'll close this issue: there's no concrete work item here as just copying a certain V8 feature "as is" is a bit too vague. If you run into concrete problems, you can open a separate issue for those.
from duktape.
Related Issues (20)
- Is nested duk_pcall_* from the same thread supported? HOT 1
- Instructions for compiling CBOR?
- CBOR vs. JSON performance -- why is CBOR.encode so much slower than JSON.stringify? HOT 5
- An issue with breaking out of labeled blocks
- An issue with undefined and postfix operators
- stack-overflow in duktape/duk_js_call.c:1361 in duk__call_setup_act_attempt_tailcall
- stack-overflow in duktape/duk_api_stack.c:337 in duk_is_valid_posidx
- stack-overflow in duk__try_push_vsprintf in duktape/duk_api_stack.c:4800:8
- stack-overflow in duktape/duk_hobject_assert.c:215 in duk_hthread_assert_valid
- stack-overflow in duktape/duk_js_call.c:1570 in duk__call_setup_act_not_tailcall
- stack-overflow in duktape/duk_hobject_props.c:272 in duk__hobject_get_entry_object_stridx
- Security concern
- Help wanted with nested objects
- Iterating keys of proxy object HOT 1
- make file which also installs duktape fails HOT 1
- Determining when code finished execution when using duk_suspend
- Q: Bytecode only compilation flag
- Support App Privacy Manifest
- duk_create_heap_default not found HOT 1
- SyntaxError: unterminated statement when trying to define a regex HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from duktape.