Skip to content

Instantly share code, notes, and snippets.

View mraleph's full-sized avatar
🐌

Slava Egorov mraleph

🐌
View GitHub Profile
// Based on Martin Kustermann's version and adding support for
// finalization of memory mapped views.
 
import 'dart:ffi';
import 'dart:io';
import 'dart:typed_data';
import 'dart:math' as math;
 
import 'package:ffi/ffi.dart';
 
import 'dart:mirrors';
class Proxy {
static final getName = currentMirrorSystem().getName;
final target;
Proxy(this.target);
noSuchMethod(invocation) {
class Nothing { const Nothing(); }
class Pipeable {
var value = const Nothing();
operator |(f) {
if (f == const Nothing()) {
return value;
} else if (value == const Nothing()) {
value = f();
} else {
value = f(value);
@mraleph
mraleph / ffi.md
Last active November 11, 2023 14:44

Dart VM FFI Vision

Background

The aim of Dart FFI project (tracked as Issue #34452) is to provide a low boilerplate, low ceremony & low overhead way of interoperating with native C/C++ code.

The motivation behind this project is twofold:

$ cat test.js
function foo () { while (true) { } }
function bar () { return foo(); }
bar();
$ node test.js &
$ gdb attach $(pidof node)
0x00000bf778c63d5f in ?? ()
(gdb) b v8::internal::Runtime_StackGuard
Breakpoint 1 at 0x84a1f0
(gdb) print 'v8::V8::TerminateExecution'(0)
@mraleph
mraleph / main.dart
Created May 30, 2023 09:56
vagrant-flurry-5605
class X {
final int Function() foo;
const X({this.foo = _defaultFoo});
static int _defaultFoo() => 0;
}
void main() {
print(const X().foo());
@mraleph
mraleph / gist:3397008
Created August 19, 2012 18:48
simple and incomplete explanation of V8 strings

There are two types (actually more, but for the problem at hand only these two are important):

  • flat strings are immutable arrays of characters

  • cons strings are pairs of strings, result of concatenation.

If you concat a and b you get a cons-string (a, b) that represents result of concatenation. If you later concat d to that you get another cons-string ((a, b), d).

Indexing into such a "tree-like" string is not O(1) so to make it faster V8 flattens the string when you index: copies all characters into a flat string.

new Fn(...) vs Object.create(P)

Basics of object layout in V8

Each JavaScript object in V8 looks like this

+-------+
|  map  | -> pointer to the hidden class
+-------+

Making iteration faster in Array built-ins.

Lets imagine we have forEach method. The core loop of this method in V8 is implemented in JavaScript and looks like this:

for (var i = 0; i < length; i++) {
  if (i in array) {
    var element = array[i];
    %_CallFunction(receiver, element, i, array, f);
 }
library mirrors.src.decode;
import 'dart:mirrors' as mirrors;
import 'dart:convert';
/// Create an object of the given type [t] from the given JSON string.
decode(String json, Type t) {
// Get deserialization descriptor for the given type.
// This descriptor describes how to handle the type and all its fields.
final TypeDesc desc = getDesc(t);