Skip to content

Instantly share code, notes, and snippets.

View dherman's full-sized avatar

Dave Herman dherman

View GitHub Profile
import { } from "node"; // have to do this for some legacy Node reason I don't understand
Object.defineProperties(Array.prototype, {
takeWhile: {
configurable: true,
writable: true,
enumerable: false,
value: function() { /* ... */ }
},
dropWhile: {
declare_types! {
/// A simple native class for creating greeting strings.
pub class JsGreeter for Greeter {
init(greeting: String) {
Greeter {
greeting: greeting
}
}
call(self) -> f64 {
@dherman
dherman / 0-test.md
Last active April 22, 2016 06:36
playing with embedded images in a gist

Hello, world.

before transpiling

Hello again.

#[doc(hidden)]
#[macro_export]
macro_rules! class_definition {
( $cls:ident ; $typ: ident ; $allocator:tt ; $mnames:tt ; $mdefs:tt ; init($call:ident) $body:block $($rest:tt)* ) => {
fn allocator_definition($call: $crate::vm::FunctionCall<$crate::js::JsUndefined>) -> $crate::vm::VmResult<$typ> {
$body
}
class_definition!($cls ; $typ ; allocator_definition ; $mnames ; $mdefs ; $($rest)*);
};
@dherman
dherman / greeter.rs
Created March 4, 2016 07:00
my first working neon class
#[macro_use]
extern crate neon;
extern crate neon_sys;
use neon::mem::Handle;
use neon::vm;
use neon::js::{JsFunction, JsString, Value, Object};
use neon::js::class::{JsClass, Class};
struct Greeter {

Defining Branded Class Types with V8

I'll use the term branded class to be a class hierarchy with:

  • a unique internal tag shared by all instances that reliably distinguishes instances from any other objects
  • a common set of internal properties
  • a fresh constructor/prototype per realm
  • brand compatibility across realms: methods that operate on instances of the branded class work on objects of the same branded class that come other realms

Examples: Date, Array, Promise, DOM classes

export default class A { ... }
import B from "B";
A.B_INSTANCE = new B();
import * as fs from "fs";
var Export = Reflect.Module.Export;
var y, TAU;
// reflective API takes Export objects to represent each export slot
var mod = new Reflect.Module({
// initialized mutable (includes "uninitialized" var)
x: Export.var(),
var addon = require('../native');
console.log(addon.test({ foo: ["bar"] }));
@dherman
dherman / neon_class_fantasy.rs
Last active February 19, 2016 00:27
a vague fantasy of what creating classes with Neon should look like
struct Greeter {
greeting: String
}
#[derive(Copy, Clone)]
struct JsGreeter(raw::Local);
impl Managed for JsGreeter {
fn to_raw(self) -> raw::Local {
let JsGreeter(raw) = self;