Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions .changeset/pink-pots-fold.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
"@lynx-js/web-mainthread-apis": patch
"@lynx-js/web-core": patch
---

refactor: isolate the globalThis in mts

After this commit, developers' mts code won't be able to access the globalThis

The following usage will NOT work

```
globalThis.foo = () =>{};
foo();//crash
```
51 changes: 51 additions & 0 deletions packages/web-platform/web-core/src/utils/loadTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,57 @@ return module.exports;}`,
const mainThreadInjectVars = [
'lynx',
'globalThis',
'__AddConfig',
'__AddDataset',
'__GetAttributes',
'__GetComponentID',
'__GetDataByKey',
'__GetDataset',
'__GetElementConfig',
'__GetElementUniqueID',
'__GetID',
'__GetTag',
'__SetAttribute',
'__SetConfig',
'__SetDataset',
'__SetID',
'__UpdateComponentID',
'__GetConfig',
'__UpdateListCallbacks',
'__AppendElement',
'__ElementIsEqual',
'__FirstElement',
'__GetChildren',
'__GetParent',
'__InsertElementBefore',
'__LastElement',
'__NextElement',
'__RemoveElement',
'__ReplaceElement',
'__ReplaceElements',
'__SwapElement',
'__CreateComponent',
'__CreateElement',
'__CreatePage',
'__CreateView',
'__CreateText',
'__CreateRawText',
'__CreateImage',
'__CreateScrollView',
'__CreateWrapperElement',
'__CreateList',
'__AddEvent',
'__GetEvent',
'__GetEvents',
'__SetEvents',
'__AddClass',
'__SetClasses',
'__GetClasses',
'__AddInlineStyle',
'__SetInlineStyles',
'__SetCSSId',
'__OnLifecycleEvent',
'__FlushElementTree',
];

const backgroundInjectVars = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,7 @@ export class MainThreadRuntime {
}

get globalThis() {
const global = Object.assign(globalThis, this);
Object.defineProperty(
global,
'renderPage',
Object.getOwnPropertyDescriptor(this, 'renderPage')!,
);
return global;
return this;
}

lynx: MainThreadLynx;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"styleInfo": {},
"lepusCode": {
"root": "globalThis.runtime = lynx_runtime;globalThis.__lynx_worker_type = 'main'",
"root": "self.runtime = lynx_runtime;self.__lynx_worker_type = 'main'",
"/manifest-chunk.js": "module.exports = 'hello';",
"/manifest-chunk2.js": "module.exports = 'world';"
},
Expand Down
1 change: 1 addition & 0 deletions packages/web-platform/web-tests/tests/react.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1229,6 +1229,7 @@ test.describe('reactlynx3 tests', () => {
'basic-element-text-set-native-props-with-setData',
async ({ page }, { title }) => {
await goto(page, title);
await wait(200);
// --initialtextinitial
let count = await page.getByText('--').count();
expect(count).toBe(1);
Expand Down
14 changes: 7 additions & 7 deletions packages/web-platform/web-tests/tests/web-core.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ test.describe('web core tests', () => {
await goto(page);
const mainWorker = await getMainThreadWorker(page);
await mainWorker.evaluate(() => {
globalThis.renderPage = () => {
const root = __CreatePage('0', '0', {});
const element = __CreateElement('view', '0', {});
__AppendElement(root, element);
const component = __CreateComponent(
globalThis.runtime.renderPage = () => {
const root = globalThis.runtime.__CreatePage('0', '0', {});
const element = globalThis.runtime.__CreateElement('view', '0', {});
globalThis.runtime.__AppendElement(root, element);
const component = globalThis.runtime.__CreateComponent(
'1',
'0-13826000',
'0',
Expand All @@ -68,8 +68,8 @@ test.describe('web core tests', () => {
{},
{},
);
__AddClass(component, 'wrapper');
__AppendElement(element, component);
globalThis.runtime.__AddClass(component, 'wrapper');
globalThis.runtime.__AppendElement(element, component);
};
});
const backWorker = await getBackgroundThreadWorker(page);
Expand Down
Loading