Skip to content

Instantly share code, notes, and snippets.

View y21's full-sized avatar
🧊

Timo y21

🧊
  • Germany
  • 04:43 (UTC +02:00)
View GitHub Profile
@y21
y21 / main.rs
Last active December 31, 2022 15:32
use llvm_sys::analysis::*;
use llvm_sys::core::*;
use llvm_sys::execution_engine::*;
use llvm_sys::prelude::*;
use llvm_sys::target::*;
use llvm_sys::*;
use std::ptr;
macro_rules! c {
($s:expr) => {
@y21
y21 / boa.rs
Last active February 19, 2022 21:43
Boa
use std::{
alloc::{GlobalAlloc, Layout},
ptr::{self},
};
use boa::JsString;
struct BadAllocator<A: GlobalAlloc>(A);
// Implementing the GlobalAlloc trait is unsafe by nature because one could "just" return any invalid pointer
@y21
y21 / abortable.rs
Created August 8, 2021 03:09
Rust abortable Future
use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};
use std::time::Duration;
#[tokio::main]
async fn main() {
struct Abortable<T, Fut: Future<Output = T> + Unpin> {
aborted: bool,
future: Fut,
@y21
y21 / lib.rs
Last active January 15, 2023 16:11
branch prediction hint intrinsics in stable rust
/// std::intrinsics::{likely, unlikely, assume} in stable Rust
mod predict {
#[inline(never)]
#[cold]
fn unlikely_inner() {}
pub fn unlikely<B: Into<bool>>(b: B) -> bool {
let b = b.into();
if b { unlikely_inner(); }
b
@y21
y21 / manual_drop.rs
Created March 19, 2021 07:33
ManualDrop
use std::ops::{Drop, Deref};
#[derive(Debug)]
struct ManualDrop<T>(*mut T);
impl<T> ManualDrop<T> {
pub fn new(data: T) -> ManualDrop<T> {
Self(Box::into_raw(Box::new(data)))
}
}
@y21
y21 / crc.json
Created March 17, 2021 09:45
crc32
[0,1996959894,3993919788,2567524794,124634137,1886057615,3915621685,2657392035,249268274,2044508324,3772115230,2547177864,162941995,2125561021,3887607047,2428444049,498536548,1789927666,4089016648,2227061214,450548861,1843258603,4107580753,2211677639,325883990,1684777152,4251122042,2321926636,335633487,1661365465,4195302755,2366115317,997073096,1281953886,3579855332,2724688242,1006888145,1258607687,3524101629,2768942443,901097722,1119000684,3686517206,2898065728,853044451,1172266101,3705015759,2882616665,651767980,1373503546,3369554304,3218104598,565507253,1454621731,3485111705,3099436303,671266974,1594198024,3322730930,2970347812,795835527,1483230225,3244367275,3060149565,1994146192,31158534,2563907772,4023717930,1907459465,112637215,2680153253,3904427059,2013776290,251722036,2517215374,3775830040,2137656763,141376813,2439277719,3865271297,1802195444,476864866,2238001368,4066508878,1812370925,453092731,2181625025,4111451223,1706088902,314042704,2344532202,4240017532,1658658271,366619977,2362670323,4224994405
@y21
y21 / vm2.js
Last active April 6, 2023 07:16
vm2
const {NodeVM} = require('vm2');
const {isMainThread, Worker, parentPort} = require('worker_threads');
if (isMainThread) {
const worker = new Worker(__filename);
let lastMessage = null;
worker.on('message', () => lastMessage = Date.now());
worker.on('online', () => lastMessage = Date.now());
@y21
y21 / ivm.js
Last active September 30, 2020 13:32
const {Isolate} = require('isolated-vm');
const isolate = new Isolate({memoryLimit: 16});
const ctx = isolate.createContextSync();
ctx.eval("Math.max('1'.repeat(1024 ** 2 * 127));");
process.exit();
@y21
y21 / example.jsx
Created September 20, 2020 12:39
Embedded console
// https://github.com/y21/embedded-console
import EmbeddedConsole from './EmbeddedConsole';
import React from 'react';
export default class Console extends React.Component {
state = {};
componentDidMount() {
const element = document.getElementById('console');
if (!element) return console.error('Console element not found');
@y21
y21 / index.js
Created September 8, 2020 19:11
pagination w editOrReply
const { Paginator } = require('detritus-pagination');
const { ShardClient } = require('detritus-client');
const client = new ShardClient('token');
const paginator = new Paginator(client, {
maxTime: 300000,
// ...
});
// Wrapper function