-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of github.com:sass/embedded-host-node into featur…
…e.color-4
- Loading branch information
Showing
60 changed files
with
369 additions
and
198 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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"extends": "./node_modules/gts/", | ||
"rules": { | ||
"@typescript-eslint/explicit-function-return-type": [ | ||
"error", | ||
{"allowExpressions": true} | ||
], | ||
"func-style": ["error", "declaration"], | ||
"prefer-const": ["error", {"destructuring": "all"}], | ||
// It would be nice to sort import declaration order as well, but that's not | ||
// autofixable and it's not worth the effort of handling manually. | ||
"sort-imports": ["error", {"ignoreDeclarationSort": true}], | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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,25 @@ | ||
#!/usr/bin/env node | ||
|
||
import * as child_process from 'child_process'; | ||
import {compilerCommand} from '../lib/src/compiler-path'; | ||
|
||
// TODO npm/cmd-shim#152 and yarnpkg/berry#6422 - If and when the package | ||
// managers support it, we should make this a proper shell script rather than a | ||
// JS wrapper. | ||
|
||
try { | ||
child_process.execFileSync( | ||
compilerCommand[0], | ||
[...compilerCommand.slice(1), ...process.argv.slice(2)], | ||
{ | ||
stdio: 'inherit', | ||
windowsHide: true, | ||
} | ||
); | ||
} catch (error) { | ||
if (error.code) { | ||
throw error; | ||
} else { | ||
process.exitCode = error.status; | ||
} | ||
} |
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,102 @@ | ||
// Copyright 2024 Google LLC. Use of this source code is governed by an | ||
// MIT-style license that can be found in the LICENSE file or at | ||
// https://opensource.org/licenses/MIT. | ||
|
||
import * as fs from 'fs'; | ||
|
||
/** Read a chunk of data from a file descriptor into a new Buffer. */ | ||
function readFileDescriptor( | ||
fd: number, | ||
position: number, | ||
length: number | ||
): Buffer { | ||
const buffer = Buffer.alloc(length); | ||
let offset = 0; | ||
while (offset < length) { | ||
const bytesRead = fs.readSync(fd, buffer, { | ||
offset: offset, | ||
position: position + offset, | ||
}); | ||
if (bytesRead === 0) { | ||
throw new Error(`failed to read fd ${fd}`); | ||
} | ||
|
||
offset += bytesRead; | ||
} | ||
return buffer; | ||
} | ||
|
||
/** Parse an ELF file and return its interpreter. */ | ||
export function getElfInterpreter(path: string): string { | ||
const fd = fs.openSync(path, 'r'); | ||
try { | ||
const elfIdentification = new DataView( | ||
readFileDescriptor(fd, 0, 64).buffer | ||
); | ||
|
||
if ( | ||
elfIdentification.getUint8(0) !== 0x7f || | ||
elfIdentification.getUint8(1) !== 0x45 || | ||
elfIdentification.getUint8(2) !== 0x4c || | ||
elfIdentification.getUint8(3) !== 0x46 | ||
) { | ||
throw new Error(`${path} is not an ELF file.`); | ||
} | ||
|
||
const elfIdentificationClass = elfIdentification.getUint8(4); | ||
if (elfIdentificationClass !== 1 && elfIdentificationClass !== 2) { | ||
throw new Error(`${path} has an invalid ELF class.`); | ||
} | ||
const elfClass32 = elfIdentificationClass === 1; | ||
|
||
const elfIdentificationData = elfIdentification.getUint8(5); | ||
if (elfIdentificationData !== 1 && elfIdentificationData !== 2) { | ||
throw new Error(`${path} has an invalid endianness.`); | ||
} | ||
const littleEndian = elfIdentificationData === 1; | ||
|
||
// Converting BigUint64 to Number because node Buffer length has to be | ||
// number type, and we don't expect any elf we check with this method to | ||
// be larger than 9007199254740991 bytes. | ||
const programHeadersOffset = elfClass32 | ||
? elfIdentification.getUint32(28, littleEndian) | ||
: Number(elfIdentification.getBigUint64(32, littleEndian)); | ||
const programHeadersEntrySize = elfClass32 | ||
? elfIdentification.getUint16(42, littleEndian) | ||
: elfIdentification.getUint16(54, littleEndian); | ||
const programHeadersEntryCount = elfClass32 | ||
? elfIdentification.getUint16(44, littleEndian) | ||
: elfIdentification.getUint16(56, littleEndian); | ||
|
||
const programHeaders = new DataView( | ||
readFileDescriptor( | ||
fd, | ||
programHeadersOffset, | ||
programHeadersEntrySize * programHeadersEntryCount | ||
).buffer | ||
); | ||
for (let i = 0; i < programHeadersEntryCount; i++) { | ||
const byteOffset = i * programHeadersEntrySize; | ||
const segmentType = programHeaders.getUint32(byteOffset, littleEndian); | ||
if (segmentType !== 3) continue; // 3 is PT_INTERP, the interpreter | ||
|
||
const segmentOffset = elfClass32 | ||
? programHeaders.getUint32(byteOffset + 4, littleEndian) | ||
: Number(programHeaders.getBigUint64(byteOffset + 8, littleEndian)); | ||
const segmentFileSize = elfClass32 | ||
? programHeaders.getUint32(byteOffset + 16, littleEndian) | ||
: Number(programHeaders.getBigUint64(byteOffset + 32, littleEndian)); | ||
|
||
const buffer = readFileDescriptor(fd, segmentOffset, segmentFileSize); | ||
if (buffer[segmentFileSize - 1] !== 0) { | ||
throw new Error(`${path} is corrupted.`); | ||
} | ||
|
||
return buffer.toString('utf8', 0, segmentFileSize - 1); | ||
} | ||
|
||
throw new Error(`${path} does not contain an interpreter entry.`); | ||
} finally { | ||
fs.closeSync(fd); | ||
} | ||
} |
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.