Skip to content

Instantly share code, notes, and snippets.

View ghaiklor's full-sized avatar
🚧
Under wartime

Eugene Obrezkov ghaiklor

🚧
Under wartime
View GitHub Profile
@ghaiklor
ghaiklor / Module_require.js
Created February 3, 2017 16:26
Module.prototype.require() sources
Module.prototype.require = function(path) {
assert(path, 'missing path');
assert(typeof path === 'string', 'path must be a string');
return Module._load(path, this, /* isMain */ false);
};
@ghaiklor
ghaiklor / Module_compile.js
Created February 3, 2017 16:33
Module.prototype._compile() sources
// Run the file contents in the correct scope or sandbox. Expose
// the correct helper variables (require, module, exports) to
// the file.
// Returns exception, if any.
Module.prototype._compile = function(content, filename) {
// Remove shebang
var contLen = content.length;
if (contLen >= 2) {
if (content.charCodeAt(0) === 35/*#*/ &&
content.charCodeAt(1) === 33/*!*/) {
@ghaiklor
ghaiklor / Module_load.js
Created February 3, 2017 18:37
Module._load sources
// Check the cache for the requested file.
// 1. If a module already exists in the cache: return its exports object.
// 2. If the module is native: call `NativeModule.require()` with the
// filename and return the result.
// 3. Otherwise, create a new module for the file and save it to the cache.
// Then have it load the file contents before returning its exports
// object.
Module._load = function(request, parent, isMain) {
if (parent) {
debug('Module._load REQUEST %s parent: %s', request, parent.id);
@ghaiklor
ghaiklor / ModuleLoad.js
Created February 3, 2017 18:41
Module.prototype.load sources
// Given a file name, pass it to the proper extension handler.
Module.prototype.load = function(filename) {
debug('load %j for module %j', filename, this.id);
assert(!this.loaded);
this.filename = filename;
this.paths = Module._nodeModulePaths(path.dirname(filename));
var extension = path.extname(filename) || '.js';
if (!Module._extensions[extension]) extension = '.js';
@ghaiklor
ghaiklor / ModuleExtensionsNode.js
Created February 3, 2017 18:43
Module._extensions.node sources
//Native extension for .node
Module._extensions['.node'] = function(module, filename) {
return process.dlopen(module, path._makeLong(filename));
};
@ghaiklor
ghaiklor / DLOpen.cc
Created February 3, 2017 18:45
process.dlopen sources
// DLOpen is process.dlopen(module, filename).
// Used to load 'module.node' dynamically shared objects.
void DLOpen(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
uv_lib_t lib;
CHECK_EQ(modpending, nullptr);
if (args.Length() != 2) {
env->ThrowError("process.dlopen takes exactly 2 arguments.");
@ghaiklor
ghaiklor / boot.asm
Created October 21, 2017 09:16
Simple boot sector
org 0x7C00 ; BIOS loads our programm at this address
bits 16 ; We're working at 16-bit mode here
start:
cli ; Disable the interrupts
mov si, msg ; SI now points to our message
mov ah, 0x0E ; Indicate BIOS we're going to print chars
.loop lodsb ; Loads SI into AL and increments SI [next char]
or al, al ; Checks if the end of the string
jz halt ; Jump to halt if the end
@ghaiklor
ghaiklor / hello-world-bootloader.sh
Created October 21, 2017 09:32
Prepares an environment with nasm/QEMU and copies basic bootloader
brew install nasm
brew install qemu
curl https://gist.githubusercontent.com/ghaiklor/89e243a3463569480d01188c9e55e077/raw/460037f466928fd2e1d2e10473a579133434c094/boot.asm > boot.asm
nasm boot.asm -f bin -o boot.bin
qemu-system-i386 -fda boot.bin
@ghaiklor
ghaiklor / disk-read.asm
Last active August 6, 2021 18:54
Read from hard drive implementation in Assembly
disk_read:
;; store all register values
pusha
push dx
;; prepare data for reading the disk
;; al = number of sectors to read (1 - 128)
;; ch = track/cylinder number
;; dh = head number
;; cl = sector number
@ghaiklor
ghaiklor / hello-world-without-libc.c
Last active April 20, 2019 14:54
Print alphabet without libc
extern void loader_main() {
for (int i = 0; i < 26; i++) {
char c = 0x41 + i;
asm(
"mov %0, %%al;"
"mov $0x0E, %%ah;"
"int $0x10;"
:
: "r" (c)