Skip to content
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

Object property defined with Object.defineProperty gets immediately evaluated #197

Open
afdev82 opened this issue Apr 27, 2021 · 5 comments

Comments

@afdev82
Copy link

afdev82 commented Apr 27, 2021

Hello,

I hope this is the right place to ask...
I have a lot of components defined in different files and in my ruby code I'm loading them using the load function:

def initialize_js(config, locale = nil)
    ctx = MiniRacer::Context.new

    ...

    Dir.entries(path.join('auto', 'utils')).sort.each do |ent|
      next if ent.start_with? '.'

      if ent.end_with? '.js'
        ctx.load(path.join('auto', 'utils', ent))
      else
        next unless File.directory?(path.join('auto', 'utils', ent))

        Dir.entries(path.join('auto', 'utils', ent)).sort.each do |ent2|
          next if ent2.start_with? '.'

          ctx.load(path.join('auto', 'utils', ent, ent2))
        end
      end
    end

    ...

    ctx
  end

I moved outside of the component's init function the definition of a property:

Before:

utils.components.accessory_power_adapter = {
  init: function(config, components) {
    if(this.initialized) return;

    Object.defineProperty(components, 'accessory_power_adapter', {
      enumerable: true,
      get: function() {
        return this.component;
      }.bind(utils.components.accessory_power_adapter)
    });

    // Initialize component
    this._init_component(config, components);

    this.initialized = true;
  },

...

};

After:

utils.components.accessory_power_adapter = {
  init: function(config, components) {
    if(this.initialized) return;

    // Initialize component
    this._init_component(config, components);

    this.initialized = true;
  },

...

};

Object.defineProperty(components, 'accessory_power_adapter', {
  enumerable: true,
  get: function() {
    if(!this.initialized) {
      throw 'call to components.accessory_power_adapter but the component is not initialized yet!';
    }

    return this.component;
  }.bind(utils.components.accessory_power_adapter)
});

And now I'm getting the exception

call to components.accessory_power_adapter but the component is not initialized yet!'

when I simply load the file in the context.
It looks to me that somehow after defining the property, the engine tries to access it and since the component is not initialized yet, I get the exception.
I don't know if this is a bug or it's the expected behavior, I'm running the same code in the browser and it's fine.
If I make the property not enumerable it doesn't happen, but I need the property to be enumerable, so the only workaround is to change the code to skip that check if I'm running the code server side:

Object.defineProperty(components, 'accessory_power_adapter', {
  enumerable: true,
  get: function() {
    if(!this.initialized && !ruby) {
      throw 'call to components.accessory_power_adapter but the component is not initialized yet!';
    }

    return this.component;
  }.bind(utils.components.accessory_power_adapter)
});

But I wanted to know why it is happening, if I've found a bug or I'm doing something wrong.
Thank you for your support!

mini_racer 0.4.0
libv8-node (15.14.0.0)
@SamSaffron
Copy link
Collaborator

Can you craft a minimal repro in an rb file? Keep in mind load is a simple wrapper around eval.

@afdev82
Copy link
Author

afdev82 commented Apr 28, 2021

Yes, I saw the definition of the load method and at the end it calls eval, there is also a comment there, I don't know if it's related to this issue.

Please find example files attached.
define_property.zip

I saw that if I call the method utils.components.accessory_power_adapter.init inside the define_property.js (before or after Object.defineProperty) I don't have the exception. It doesn't help anyway, because I need to call the function in a different file.

@SamSaffron
Copy link
Collaborator

It looks to me that somehow after defining the property, the engine tries to access it and since the component is not initialized yet, I get the exception.

I think I know what is happening here. The last element in a script is returned to the caller of eval.

I guess a fix here would be to have load use a special flavor of eval that does not try to return the caller.

A simple workaround would be a custom eval function:

def eval2(ctx, js)
    js = js + "\n;true;"
   ctx.eval(js)
end

Happy to fix up load here so it does not evaluate.

@afdev82
Copy link
Author

afdev82 commented May 3, 2021

Thank you for your answer :)
I'm glad to help if I can.

@tisba
Copy link
Collaborator

tisba commented Sep 29, 2022

Has this ever been implemented, @SamSaffron? Having load not evaluate?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants