-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
204 additions
and
139 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,90 @@ | ||
const WBC = require('./'); | ||
const { compileJavaScriptToWbc } = require('./'); | ||
const Qjsc = require('./qjsc'); | ||
const path = require('path'); | ||
|
||
const wbc = new WBC(); | ||
const testCode = '1 + "3"'; | ||
|
||
describe('wbc', () => { | ||
it('throw error when empty arguments', () => { | ||
expect(() => wbc.compile()).toThrowError('1st arguments should be string.'); | ||
}); | ||
describe('compileJavaScriptToWbc', () => { | ||
const rootDir = path.resolve(__dirname, '.'); | ||
const bindings = require('node-gyp-build')(rootDir); | ||
const signatureSize = 9; | ||
|
||
describe('Header', () => { | ||
it('header should contains signature', async () => { | ||
const bytes = compileJavaScriptToWbc(testCode); | ||
const uint8Array = new Uint8Array(bytes); | ||
expect(Array.from(uint8Array.slice(0, 9))).toEqual([0x89, 0x57, 0x42, 0x43, 0x31, 0x0D, 0x0A, 0x1A, 0x0A]); | ||
}); | ||
|
||
it('validate the header chunk header', () => { | ||
const bytes = compileJavaScriptToWbc(testCode); | ||
const uint8Array = new Uint8Array(bytes); | ||
expect([...uint8Array.slice(9, 13)]).toEqual([0, 0, 0, 18]); | ||
}); | ||
|
||
it('validate the chunk type', () => { | ||
const bytes = compileJavaScriptToWbc(testCode); | ||
const uint8Array = new Uint8Array(bytes); | ||
expect([...uint8Array.slice(13, 17)]).toEqual([0x57, 0x42, 0x48, 0x44]); | ||
}); | ||
|
||
it('validate compression method', () => { | ||
const bytes = compileJavaScriptToWbc(testCode); | ||
const uint8Array = new Uint8Array(bytes); | ||
expect([...uint8Array.slice(17, 18)]).toEqual([0]); | ||
}); | ||
|
||
it('throw error when js syntax not correct', () => { | ||
const code = ` | ||
function f() { | ||
it('validate compile level', () => { | ||
const bytes = compileJavaScriptToWbc(testCode); | ||
const uint8Array = new Uint8Array(bytes); | ||
expect([...uint8Array.slice(18, 19)]).toEqual([0]); | ||
}); | ||
|
||
console.log(111; | ||
it('validate bytecode version', () => { | ||
const bytes = compileJavaScriptToWbc(testCode); | ||
const uint8Array = new Uint8Array(bytes); | ||
expect([...uint8Array.slice(19, 20)]).toEqual([0]); | ||
}); | ||
|
||
`; | ||
expect(() => wbc.compile(code)).toThrowError(); | ||
it('validate additional data', () => { | ||
const bytes = compileJavaScriptToWbc(testCode); | ||
const uint8Array = new Uint8Array(bytes); | ||
expect([...uint8Array.slice(20, 23)]).toEqual([0, 0, 0]); | ||
}); | ||
|
||
it('validate header check sum', () => { | ||
const bytes = compileJavaScriptToWbc(testCode); | ||
const uint8Array = new Uint8Array(bytes); | ||
const headerLength = uint8Array[12]; | ||
const bodyOffset = signatureSize + headerLength; | ||
const headerCheckSumOffset = bodyOffset - 4; | ||
const checksum = bindings.getAdler32(uint8Array.slice(signatureSize, headerCheckSumOffset)); | ||
const buffer = Buffer.alloc(4); | ||
buffer.writeUInt32BE(checksum, 0); | ||
|
||
expect([...new Uint8Array(buffer)]).toEqual([10, 168, 1, 56]); | ||
}); | ||
}); | ||
|
||
it('return bytecode binary', () => { | ||
const code = ` | ||
function f() { return 1 + '1234'; } | ||
f(); | ||
`; | ||
let buffer = wbc.compile(code); | ||
expect(wbc._evalByteCode(buffer)).toBe('11234'); | ||
describe('Body', () => { | ||
const bytes = compileJavaScriptToWbc(testCode); | ||
const uint8Array = new Uint8Array(bytes); | ||
const headeLength = uint8Array.slice(12, 13)[0]; | ||
const bodyHeader = uint8Array.slice(headeLength + 9); | ||
const bodyLength = bodyHeader[3]; | ||
const bodySlice = uint8Array.slice(headeLength + 9, headeLength + bodyLength + 9); | ||
|
||
it('body should contains signature', () => { | ||
expect([...bodySlice.slice(4, 8)]).toEqual([0x57, 0x42, 0x44, 0x59]); | ||
}); | ||
|
||
it('body content bytes should be executable', () => { | ||
const bytecode = bodySlice.slice(8, bodySlice.length - 4); | ||
const qjs = new Qjsc(); | ||
const result = qjs._evalByteCode(Buffer.from(bytecode.buffer)); | ||
expect(result).toBe('13'); | ||
}); | ||
}); | ||
|
||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters