Skip to content

Instantly share code, notes, and snippets.

View dherman's full-sized avatar

Dave Herman dherman

View GitHub Profile
@dherman
dherman / realms-api.md
Last active March 8, 2024 07:04
ES6 Realms API

Notational Conventions

This section describes the conventions used here to describe type signatures.

A [T] is an array-like value (only ever used read-only in this API), i.e., one with an integer length and whose indexed properties from 0 to length - 1 are of type T.

A type T? should be read as T | undefined -- that is, an optional value that may be undefined.

Realms

@dherman
dherman / shared-array-buffer.md
Last active October 29, 2023 11:04
fine-grained borrowing (read-write) and sharing (read-only) of buffers

Goal

Typed arrays can be copied or transferred between workers, but it's not possible for multiple workers to work with a buffer in parallel without copies. This document describes two ways to improve this without introducing data races: transferring read-write access to disjoint regions of buffers, and transferring read-only access to shared buffers/regions.

This variant of the API enables fine-grained borrowing and sharing, where a single ArrayBuffer can have multiple disjoint regions parceled out. This way individual workers can work with their regions at their original indices. This makes the API more amenable to being a compilation target.

Example

Here is an example that demonstrates sharing a read-only segment and multiple read-write segments with four separate workers.

@dherman
dherman / README.md
Last active July 10, 2023 19:15
How the ES6 Realm API makes it possible to create robust JS dialects

JS dialects with ES6 Realms

This essay explains how the ES6 Realm API makes it possible to create robust language abstractions that allow hooking into the behavior of eval, and how this can be used to implement different dialects of JavaScript.

Example scenario: Crock's "default operator"

Imagine we want to add Doug Crockford's ?? operator, which acts like a short-circuiting logical OR operator, except instead of checking truthiness, it returns the first argument's value if the first argument is any value other than undefined.

Since it makes everything simpler and cleaner, I'm going to assume I can use do-expressions for the implementation. (They're looking good for ES7!) So with that said, when we "crockpile" EXPR1 ?? EXPR2 we

@dherman
dherman / emacs-cheat-sheet.md
Created August 2, 2012 16:22
My emacs cheat sheet

In penance for cracking stupid jokes on Twitter, here's my Emacs cheat sheet. Emacs has a steep learning curve, so I've tried to order them by importance so you could learn them in stages.

One overall rule of thumb: pay attention to the minibuffer (the line at the bottom of the editor). It will often guide you through a process, and also gives you hints about what state you're in, such as the middle of a multi-chord sequence.

The other rule of thumb: when in doubt, C-g it out.

Basics (mandatory)

You simply can't get by without having these at your fingertips.

{"reason":"compiler-artifact","package_id":"proc-macro2 1.0.53 (registry+https://github.com/rust-lang/crates.io-index)","manifest_path":"/Users/dherman/.cargo/registry/src/github.com-1ecc6299db9ec823/proc-macro2-1.0.53/Cargo.toml","target":{"kind":["custom-build"],"crate_types":["bin"],"name":"build-script-build","src_path":"/Users/dherman/.cargo/registry/src/github.com-1ecc6299db9ec823/proc-macro2-1.0.53/build.rs","edition":"2018","doc":false,"doctest":false,"test":false},"profile":{"opt_level":"0","debuginfo":2,"debug_assertions":true,"overflow_checks":true,"test":false},"features":["proc-macro"],"filenames":["/Users/dherman/Sources/neon-prebuild-example/target/debug/build/proc-macro2-7bfb42c5b5753c49/build-script-build"],"executable":null,"fresh":true}
{"reason":"build-script-executed","package_id":"proc-macro2 1.0.53 (registry+https://github.com/rust-lang/crates.io-index)","linked_libs":[],"linked_paths":[],"cfgs":["use_proc_macro","wrap_proc_macro"],"env":[],"out_dir":"/Users/dherman/Sources/neon-prebuil
@dherman
dherman / ssa-cps-anf.txt
Created November 20, 2012 03:10
links on SSA, CPS, and A-normal form
Original paper on A-normal form:
http://redlinernotes.com/docs/Classics/The%20Essence%20of%20Compiling%20with%20Continuations.pdf
A high-level intro to ANF:
http://matt.might.net/articles/a-normalization/
One of the earlier attempts to relate SSA and CPS:
@dherman
dherman / linkify.rs
Created January 8, 2023 00:38
small example of exposing a Rust crate's functionality through tasks and promises
use neon::prelude::*;
use neon::types::buffer::TypedArray;
use linkify::{LinkFinder, LinkKind, Span};
// Purely indexed version of Span<'t> that removes reference to the original string.
struct IndexSpan {
start: u32,
end: u32,
kind: u32,
}
dherman@dherman-mn2 neon % npm test
> test
> npm run test:rust && npm run test:js
> test:rust
> cargo neon-test
Compiling neon v1.0.0-alpha.1 (/Users/dherman/Sources/neon/crates/neon)
/*
* https://users.rust-lang.org/t/how-to-implement-index-range-usize-for-a-custom-slice-type-with-additional-data/66201
*
* buffer.region(4, 16).into::<JsUint32Array>
* buffer.from(4).to(16).into::<JsUint32Array>
* buffer[4..16].to_typed_array::<u32, _>(&mut cx)
* JsUint32Array::from_region(&mut cx, buffer[4..16])
* buffer.region(4..16).to_typed_array::<u32, _>(&mut cx)
* JsUint32Array::from_region(&mut cx, buffer.region(4..16))
* JsUint32Array::from_buffer_region(&mut cx, buffer, 4, 16))
trait TypedArrayExt {
fn set_info<'cx, C: Context<'cx>>(&self, cx: &mut C, info: Handle<'cx, JsObject>) -> NeonResult<()>;
fn get_info<'cx, C: Context<'cx>>(&self, cx: &mut C) -> JsResult<'cx, JsObject> {
let info = cx.empty_object();
self.set_info(cx, info)?;
Ok(info)
}
}