Skip to content

Commit

Permalink
Merge pull request #60 from openkraken/fix/window_set_property
Browse files Browse the repository at this point in the history
fix: fix set property to window did't refer to globalThis.
  • Loading branch information
yuanyan authored Mar 16, 2021
2 parents 28c9e86 + 2eee5ea commit 0241783
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
20 changes: 20 additions & 0 deletions bridge/bindings/jsc/KOM/window.cc
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,26 @@ JSValueRef WindowInstance::getProperty(std::string &name, JSValueRef *exception)
return nullptr;
}

bool WindowInstance::setProperty(std::string &name, JSValueRef value, JSValueRef *exception) {
auto propertyMap = getWindowPropertyMap();
auto prototypePropertyMap = getWindowPrototypePropertyMap();
JSStringHolder nameStringHolder = JSStringHolder(context, name);

// Key is prototype property, return false to handled by engine itself.
if (prototypePropertyMap.count(name) > 0) {
return false;
}

// Key is window's built-in property. return true to do nothing because this properties are readonly.
if (propertyMap.count(name) > 0) {
return true;
}

JSObjectSetProperty(_hostClass->ctx, _hostClass->context->global(), nameStringHolder.getString(), value, kJSPropertyAttributeNone, exception);

return EventTargetInstance::setProperty(name, value, exception);
}

void WindowInstance::getPropertyNames(JSPropertyNameAccumulatorRef accumulator) {
EventTargetInstance::getPropertyNames(accumulator);

Expand Down
1 change: 1 addition & 0 deletions bridge/bindings/jsc/KOM/window.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class WindowInstance : public EventTargetInstance {
~WindowInstance();

JSValueRef getProperty(std::string &name, JSValueRef *exception) override;
bool setProperty(std::string &name, JSValueRef value, JSValueRef *exception) override;
void getPropertyNames(JSPropertyNameAccumulatorRef accumulator) override;

NativeWindow *nativeWindow;
Expand Down
10 changes: 10 additions & 0 deletions integration_tests/specs/window/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ xdescribe('windowisglobal', () => {
expect(window.foo).toBe('foo');
});

it('set property are sample with set into global', () => {
// @ts-ignore
window.abc = '1234';
// @ts-ignore
expect(abc).toBe('1234');
expect(globalThis.abc).toBe('1234');
// @ts-ignore
expect(window.abc).toBe('1234');
});

it('onload should in window', () => {
expect('onload' in window).toBe(true);
});
Expand Down

0 comments on commit 0241783

Please sign in to comment.