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
13 changes: 13 additions & 0 deletions .changeset/busy-hounds-take.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
"@lynx-js/web-mainthread-apis": patch
"@lynx-js/web-worker-runtime": patch
"@lynx-js/web-core": patch
---

feat: allow lynx code to get JS engine provided properties on globalThis

```
globalThis.Reflect; // this will be the Reflect Object
```

Note that `assigning to the globalThis` is still not allowed.
8 changes: 8 additions & 0 deletions .changeset/silly-pumas-float.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@lynx-js/web-mainthread-apis": patch
"@lynx-js/web-constants": patch
"@lynx-js/web-core": patch
"@lynx-js/web-core-server": patch
---

fix: corrupt mainthread module cache
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,20 @@ export class MainThreadRuntime {
*/
__lynxGlobalBindingValues: Record<string, any> = {};

get globalThis() {
return this;
}
globalThis = new Proxy(this, {
get: (target, prop) => {
// @ts-expect-error
return target[prop] ?? globalThis[prop];
},
set: (target, prop, value) => {
// @ts-expect-error
target[prop] = value;
return true;
},
ownKeys(target) {
return Reflect.ownKeys(target).filter((key) => key !== 'globalThis');
},
});

SystemInfo: typeof systemInfo;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import {
} from './MainThreadRuntime.js';

const moduleCache: Record<string, LynxJSModule> = {};

export function loadMainThread(
backgroundThreadRpc: Rpc,
docu: Pick<Document, 'append' | 'createElement' | 'addEventListener'>,
Expand Down Expand Up @@ -65,15 +64,15 @@ export function loadMainThread(
markTimingInternal('decode_start');
const lepusCodeEntries = await Promise.all(
Object.entries(lepusCode).map(async ([name, url]) => {
const cachedModule = moduleCache[name];
const cachedModule = moduleCache[url];
if (cachedModule) {
return [name, cachedModule] as [string, LynxJSModule];
} else {
Object.assign(globalThis, { module: {} });
await import(/* webpackIgnore: true */ url);
const module = globalThis.module as LynxJSModule;
Object.assign(globalThis, { module: {} });
moduleCache[name] = module;
moduleCache[url] = module;
return [name, module] as [string, LynxJSModule];
}
}),
Expand Down Expand Up @@ -169,7 +168,7 @@ export function loadMainThread(
publicComponentEvent,
postExposure,
},
}).globalThis;
});
markTimingInternal('decode_end');
entry!(runtime);
jsContext.__start(); // start the jsContext after the runtime is created
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ function initializeMainThreadTest() {
},
postExposure: () => {},
},
});
}).globalThis;
Object.assign(globalThis, runtime);
Object.assign(globalThis, {
genFiberElementTree,
Expand Down

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions packages/web-platform/web-tests/tests/react.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,16 @@ test.describe('reactlynx3 tests', () => {
await wait(100);
await expect(page.locator('#target')).toHaveCSS('color', 'rgb(0, 0, 0)');
});
test('basic-globalThis-property-bts', async ({ page }, { title }) => {
await goto(page, title);
await wait(100);
await expect(page.locator('#target')).toHaveCSS('color', 'rgb(0, 0, 0)');
});
test('basic-globalThis-property-mts', async ({ page }, { title }) => {
await goto(page, title);
await wait(100);
await expect(page.locator('#target')).toHaveCSS('color', 'rgb(0, 0, 0)');
});
});
test.describe('apis', () => {
test('api-custom-template-loader', async ({ page }, { title }) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright 2023 The Lynx Authors. All rights reserved.
// Licensed under the Apache License Version 2.0 that can be found in the
// LICENSE file in the root directory of this source tree.
import { root } from '@lynx-js/react';

function App() {
return (
<view
id='target'
style={{
height: '100px',
width: '100px',
background: globalThis.Object ? 'green' : 'pink',
}}
/>
);
}

root.render(<App></App>);
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright 2023 The Lynx Authors. All rights reserved.
// Licensed under the Apache License Version 2.0 that can be found in the
// LICENSE file in the root directory of this source tree.
import { useState, root, useEffect } from '@lynx-js/react';
function App() {
const [color, setColor] = useState('pink');
useEffect(() => {
if (globalThis.Object) {
setColor('green');
}
}, []);
return (
<view
id='target'
style={{
height: '100px',
width: '100px',
background: color,
}}
/>
);
}
root.render(<App />);
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright 2023 The Lynx Authors. All rights reserved.
// Licensed under the Apache License Version 2.0 that can be found in the
// LICENSE file in the root directory of this source tree.
import { root } from '@lynx-js/react';

function App() {
return (
new Array(10).fill(0).map((_, i) => (
<div
key={i}
id={`target-${i}`}
style={{
height: '100px',
width: '100px',
background: 'pink',
}}
/>
))
);
}

root.render(<App></App>);
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright 2023 The Lynx Authors. All rights reserved.
// Licensed under the Apache License Version 2.0 that can be found in the
// LICENSE file in the root directory of this source tree.
import { root } from '@lynx-js/react';

function App() {
return (
new Array(100).fill(0).map((_, i) => (
<div
key={i}
id={`target-${i}`}
style={{
height: '100px',
width: '100px',
background: 'pink',
}}
/>
))
);
}

root.render(<App></App>);
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright 2023 The Lynx Authors. All rights reserved.
// Licensed under the Apache License Version 2.0 that can be found in the
// LICENSE file in the root directory of this source tree.
import { root } from '@lynx-js/react';

function App() {
return (
new Array(1000).fill(0).map((_, i) => (
<div
key={i}
id={`target-${i}`}
style={{
height: '100px',
width: '100px',
background: 'pink',
}}
/>
))
);
}

root.render(<App></App>);
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright 2023 The Lynx Authors. All rights reserved.
// Licensed under the Apache License Version 2.0 that can be found in the
// LICENSE file in the root directory of this source tree.
import { root } from '@lynx-js/react';

function App(prop) {
return (
<div
id={`target-${prop.count}`}
>
{prop.count && <App count={prop.count - 1} />}
</div>
);
}

root.render(<App count={100}></App>);
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,19 @@ async function readTemplate(name: string) {
const rawTemplate = JSON.parse(file);
return rawTemplate as any;
}
const rawTemplate = await readTemplate('basic-performance-div-10000');
const cases = {
'basic-performance-div-10000': await readTemplate(
'basic-performance-div-10000',
),
'basic-performance-div-1000': await readTemplate(
'basic-performance-div-1000',
),
'basic-performance-div-100': await readTemplate('basic-performance-div-100'),
'basic-performance-div-10': await readTemplate('basic-performance-div-10'),
'basic-performance-nest-level-100': await readTemplate(
'basic-performance-nest-level-100',
),
};
describe('server-tests', () => {
bench('basic-performance-div-10000', async () => {
const lynxView = createLynxView({
Expand All @@ -26,7 +38,63 @@ describe('server-tests', () => {
tagMap: {},
initData: {},
globalProps: {},
template: rawTemplate,
template: cases['basic-performance-div-10000'],
});
await lynxView.renderToString();
});
bench('basic-performance-div-1000', async () => {
const lynxView = createLynxView({
browserConfig: {
pixelRatio: 1,
pixelWidth: 600,
pixelHeight: 800,
},
tagMap: {},
initData: {},
globalProps: {},
template: cases['basic-performance-div-1000'],
});
await lynxView.renderToString();
});
bench('basic-performance-div-100', async () => {
const lynxView = createLynxView({
browserConfig: {
pixelRatio: 1,
pixelWidth: 600,
pixelHeight: 800,
},
tagMap: {},
initData: {},
globalProps: {},
template: cases['basic-performance-div-100'],
});
await lynxView.renderToString();
});
bench('basic-performance-div-10', async () => {
const lynxView = createLynxView({
browserConfig: {
pixelRatio: 1,
pixelWidth: 600,
pixelHeight: 800,
},
tagMap: {},
initData: {},
globalProps: {},
template: cases['basic-performance-div-10'],
});
await lynxView.renderToString();
});
bench('basic-performance-nest-level-100', async () => {
const lynxView = createLynxView({
browserConfig: {
pixelRatio: 1,
pixelWidth: 600,
pixelHeight: 800,
},
tagMap: {},
initData: {},
globalProps: {},
template: cases['basic-performance-nest-level-100'],
});
await lynxView.renderToString();
});
Expand Down
Loading