-
Notifications
You must be signed in to change notification settings - Fork 292
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: misc cleanup in simulator (#7203)
- Loading branch information
Showing
24 changed files
with
168 additions
and
167 deletions.
There are no files selected for viewing
Validating CODEOWNERS rules …
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
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
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
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
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
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
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
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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { promisify } from 'util'; | ||
import { gunzip } from 'zlib'; | ||
|
||
import { Mov } from '../avm/opcodes/memory.js'; | ||
|
||
const AVM_MAGIC_SUFFIX = Buffer.from([ | ||
Mov.opcode, // opcode | ||
0x00, // indirect | ||
...Buffer.from('000018ca', 'hex'), // srcOffset | ||
...Buffer.from('000018ca', 'hex'), // dstOffset | ||
]); | ||
|
||
export function markBytecodeAsAvm(bytecode: Buffer): Buffer { | ||
return Buffer.concat([bytecode, AVM_MAGIC_SUFFIX]); | ||
} | ||
|
||
// This is just a helper function for the AVM simulator | ||
export async function decompressBytecodeIfCompressed(bytecode: Buffer): Promise<Buffer> { | ||
try { | ||
return await promisify(gunzip)(bytecode); | ||
} catch { | ||
// If the bytecode is not compressed, the gunzip call will throw an error | ||
// In this case, we assume the bytecode is not compressed and continue. | ||
return Promise.resolve(bytecode); | ||
} | ||
} | ||
|
||
export async function isAvmBytecode(bytecode: Buffer): Promise<boolean> { | ||
const decompressedBytecode = await decompressBytecodeIfCompressed(bytecode); | ||
const magicSize = AVM_MAGIC_SUFFIX.length; | ||
return decompressedBytecode.subarray(-magicSize).equals(AVM_MAGIC_SUFFIX); | ||
} |
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
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
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
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
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
Oops, something went wrong.