Skip to content

Instantly share code, notes, and snippets.

View RReverser's full-sized avatar
🇺🇦

Ingvar Stepanyan RReverser

🇺🇦
View GitHub Profile
// Playground link: https://www.typescriptlang.org/play?#code/PTAEHEEsGcBdQG4FMBO1IHsB2AuUALWWAB2hxAHMZYA6K2fAVwCMaBjDAW2ACUelkaVMABMAZgCMbMQDYAHAFZFAQwAmCkWwAMSACwB2VXJHN98hQu1IFAWABQ92AE9iSUAFEAHkjYAeAMJcnMpYqtAANKAAyrDKsG4AvKAA3qBwymwA1ngA2gC6kQA2GGzKhWQpAL6glQB8oAn2oKCBnMGh0KBInvEdoDmQWABmqC1BIaqRNNODIyig-HB5TaAA-B7efouwkV4+re2qvjFxSJEHE7W1K3gn8QDc9o4ubgDygiiQqki+r8wAVpEAHLKThnUAANTKjCQ9SSILBXR6SD6cE+WAoaxSKxyAGlQINQAAKTJIJwYIagP7-ACUoAAPqAEUg8nh8d1emEmaC3OsoYUYaA8NS8aAAGTE0nkynUmnLOzVPBYASoR4OOzOVwQJCwfkw34A4E8uHcxEclFcqUUqkArEi5l5IWgZWCNXPLUAFUYxEKPyBSM5nSwjE4zFQkR4AYtnRCTnyDX6eRNPByAHJfRiGKnHea+v71pG8F6fX7IjlY1Npjwk267IN4ighhk3ECkBQgSGwyhfABBZidXNc4Oh1D1VLKihxSDIPB9zqVd0ttu+D0mj1RvNtjsj7uzUZz+rrOdO9eDoOd0brVvti-d1dOl2qp4al6gOcrtcbrnX7dd3x7+Y53vI9+3XIsv3PHcsTA50VRQWtNTcHtiB9JwokgCgsBXSIAAUMAHZE+mYDAMF9EITTwgjA1AWAUEFdYYOvD8ENfcAdXQzCPwTU9CO-Ldb18Ycu0PUAm3KNw8FomEWK1ABZRhCg4rCe0iAAhE0chU0BVJzXjOhyKTwUMx1GRyMToHBcyWSxQynSsmS3CiENf1QXsIOdW81PcoTRwTHJphoYtfV7WpK0C71gvUvI0wzCgs0dCUfPg59EOiFggp+Ht3NjfIvLPUAcq
// Don't ask about this one. I have no idea what this magic is.
// Copied from https://github.com/microsoft/TypeScript/issues/13298#issuecomment-724542300.
type UnionToIntersection<U> = (
U extends any ? (arg: U) => any : never
) extends (arg: infer I) => void
? I
: never;
// Continuation of above.
type UnionToTuple<T> = UnionToIntersection<(T extends any ? (t: T) => T : never)> extends (_: any) => infer W
async function supportsImgType(type) {
// Create
//
// <picture>
// <source srcset="data:,x" type="{type}" />
// <img />
// </picture>
//
// (where "data:,x" is just a minimal URL that is valid but doesn't trigger network)
let img = document.createElement('img');
addEventListener('fetch', event => {
if (event.request.method === 'PUT') {
event.respondWith(handleRequest(event));
}
});
async function handleRequest(event) {
let response = await fetch(event.request);
if (!response.ok) return response;
let urls = ['/'];
@RReverser
RReverser / embind-asyncify.sh
Last active June 24, 2020 17:51
Embind + Asyncify
# Install tip-of-tree Emscripten
$ emsdk install tot
$ emsdk activate tot
# Build with Embind and Asyncify enabled
$ emcc --bind -s ASYNCIFY mylib.cpp -o mylib.html
const TEXT_ENCODER = new TextEncoder();
const WASM_PAGE_SIZE = 1 << 16;
function encodeStringToBinary(str) {
// Initially allocate enough space for the case where 1 char requires 1 byte (ASCII-only).
let dest = new WebAssembly.Memory({ initial: Math.ceil(str.length / WASM_PAGE_SIZE) });
let writePos = 0;
for (;;) {
// Write starting from the last written position.
// Create a Worker we want to share memory with:
let w = new Worker(`data:text/javascript,
onmessage = ({data: memory}) => {
// Got WebAssembly.Memory once, log same instance forever with no further postMessages:
setInterval(() => console.log('Current buffer in worker:', memory.buffer), 5_000);
}
`);
// Create a shared growable memory:
let m = new WebAssembly.Memory({ initial:1, maximum: 65536, shared: true });
// Send memory to the worker:
#!/usr/bin/env python
# Use with
# > cargo test --no-run # print compilation failures if any
# > RUST_TEST_THREADS=1 cargo test -q -- --logfile tests.log 2>failures.log; ../convert-test-log.py
import xml.etree.cElementTree as ET
import re
failure_re = re.compile(r"""thread '(.*?)' panicked at '(.*?)', tests/test\.rs:\d+:\d+
extern crate bincode;
extern crate cidr;
extern crate hex_slice;
extern crate rmp_serde;
extern crate serde;
extern crate serde_bytes;
extern crate serde_cbor;
#[macro_use]
extern crate serde_derive;
pub struct IpCidr(::cidr::IpCidr);
impl Debug for IpCidr {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
Debug::fmt(&self.0, f)
}
}
impl Deref for IpCidr {
type Target = ::cidr::IpCidr;