-
Notifications
You must be signed in to change notification settings - Fork 600
/
bytecode-test.js
93 lines (84 loc) 路 2.29 KB
/
bytecode-test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict-local
* @format
* @oncall react_native
*/
import type {Dependency} from '../../../types.flow';
import CountingSet from '../../../../lib/CountingSet';
import createModuleIdFactory from '../../../../lib/createModuleIdFactory';
import {wrapModule} from '../bytecode';
import {compile, validateBytecodeModule} from 'metro-hermes-compiler';
let myModule, bytecode;
beforeEach(() => {
const code = '__d(function() { console.log("foo") });';
({bytecode} = compile(
'__d(function() { console.log("foo") },$$_METRO_DEFINE_GLOBAL[0],$$_METRO_DEFINE_GLOBAL[1]);',
{sourceURL: 'test.js'},
));
myModule = {
path: '/root/foo.js',
dependencies: new Map<string, Dependency>([
[
'bar',
{
absolutePath: '/bar',
data: {data: {asyncType: null, locs: [], key: 'bar'}, name: 'bar'},
},
],
[
'baz',
{
absolutePath: '/baz',
data: {data: {asyncType: null, locs: [], key: 'baz'}, name: 'baz'},
},
],
]),
getSource: () => Buffer.from(''),
inverseDependencies: new CountingSet(),
output: [
{
data: {
code,
lineCount: 1,
map: [],
},
type: 'js/module',
},
{
data: {
bytecode,
},
type: 'bytecode/module',
},
],
};
});
it('produces a bytecode header buffer for each module', () => {
const buffers = wrapModule(myModule, {
createModuleId: createModuleIdFactory(),
dev: true,
includeAsyncPaths: false,
projectRoot: '/root',
serverRoot: '/root',
});
expect(buffers.length).toBe(2);
expect(() => validateBytecodeModule(buffers[0], 0)).not.toThrow();
expect(buffers[1]).toBe(bytecode);
});
it('does not produce a bytecode header buffer for a script', () => {
myModule.output[1].type = 'bytecode/script';
const buffers = wrapModule(myModule, {
createModuleId: createModuleIdFactory(),
dev: true,
includeAsyncPaths: false,
projectRoot: '/root',
serverRoot: '/root',
});
expect(buffers.length).toBe(1);
expect(buffers[0]).toBe(bytecode);
});