GithubHelp home page GithubHelp logo

Comments (19)

svaarala avatar svaarala commented on June 8, 2024

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.

hyunjunekim avatar hyunjunekim commented on June 8, 2024

If use Proxy, Does a object which is proxy break the proto chain?

from duktape.

hyunjunekim avatar hyunjunekim commented on June 8, 2024

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.

svaarala avatar svaarala commented on June 8, 2024

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.

hyunjunekim avatar hyunjunekim commented on June 8, 2024

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.

svaarala avatar svaarala commented on June 8, 2024

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.

hyunjunekim avatar hyunjunekim commented on June 8, 2024

I mean we should support the general Object like V8.(http://izs.me/v8-docs/classv8_1_1ObjectTemplate.html)

from duktape.

svaarala avatar svaarala commented on June 8, 2024

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.

svaarala avatar svaarala commented on June 8, 2024

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.

hyunjunekim avatar hyunjunekim commented on June 8, 2024

@svaarala Wow. I'm understand.
print(sam.proto == student) // result : true
Thank you.

from duktape.

svaarala avatar svaarala commented on June 8, 2024

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.

hyunjunekim avatar hyunjunekim commented on June 8, 2024

@svaarala If exist this API, I think very useful. So reopen this issue.
and Thank you for help. :)

from duktape.

hyunjunekim avatar hyunjunekim commented on June 8, 2024

@svaarala I have last question. Is it possible that wrap proxy in the global object?

from duktape.

svaarala avatar svaarala commented on June 8, 2024

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.

hyunjunekim avatar hyunjunekim commented on June 8, 2024

@svaarala Is this API named "duk_set_global_object" since 0.11.0?

from duktape.

svaarala avatar svaarala commented on June 8, 2024

The duk_set_global_object() call was added in 0.12.0.

from duktape.

hyunjunekim avatar hyunjunekim commented on June 8, 2024

@svaarala :) Thank you. 💯 Now, I'am happy. because of you.

from duktape.

svaarala avatar svaarala commented on June 8, 2024

Glad to help, let me know if you run into further issues :)

from duktape.

svaarala avatar svaarala commented on June 8, 2024

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.