Skip to content

Instantly share code, notes, and snippets.

View ishikawa's full-sized avatar
🏠
Working from home

Takanori Ishikawa ishikawa

🏠
Working from home
View GitHub Profile
julia> @async begin
server = listen(2000)
while true
sock = accept(server)
println("Hello World\n")
end
end
Task (queued) @0x00007fe0d62ae100
julia> connect(2000)
@ishikawa
ishikawa / putd.c
Created May 4, 2014 02:23
How to link modules and JIT execution by using ruby-llvm
#include <stdio.h>
int putd(double x) {
printf("putd: %.2f\n", x);
return 1;
}
# LLVM::JITCompiler#run_function can't handle aggregate value
engine = LLVM::JITCompiler.new(module)
function = module.functions["doSomething"]
pointer = engine.pointer_to_global(function)
fn = FFI::Function.new(:int, [:int], pointer)
fn.call(123)
@ishikawa
ishikawa / llvm_puts.rb
Created May 17, 2014 13:40
Sample POSIX "Hello World" Application (ruby-llvm)
require 'llvm/core'
require 'llvm/analysis'
require 'llvm/execution_engine'
mod = LLVM::Module.new("test")
str = "Hello, World!"
STRING = mod.globals.add(LLVM::Array(LLVM::Int8, str.size + 1), "str") do |value|
value.linkage = :internal
require 'llvm/core'
require 'llvm/analysis'
require 'llvm/execution_engine'
mod = LLVM::Module.new("test.fprintf")
str = "value = %d\n"
V_STRING = mod.globals.add(LLVM::Array(LLVM::Int8, str.size + 1), "str") do |value|
value.linkage = :internal
@ishikawa
ishikawa / llvm_ruby_ffi_callback.rb
Created May 17, 2014 23:54
Call Ruby method (FFI::Function) from LLVM
require 'ffi'
require 'llvm/core'
require 'llvm/analysis'
require 'llvm/execution_engine'
CALLBACK_TYPE = LLVM.Function([LLVM::Int], LLVM.Void)
mod = LLVM::Module.new("test.fprintf")
do_work = mod.functions.add("do_work", [CALLBACK_TYPE.pointer], LLVM.Void) do |fn, callback|
@ishikawa
ishikawa / factorial_mcjit.rb
Created May 29, 2014 23:25
ruby-llvm factorial example using MCJIT
require 'ffi'
require 'llvm/core'
require 'llvm/execution_engine'
require 'llvm/target'
# Start the "engine" before driving
# call InitializeNativeTargetAsmPrinter and InitializeNativeTargetAsmParser
LLVM::Target.init_native(true)
LLVM::C.link_in_mcjit
@ishikawa
ishikawa / find.swift
Created July 20, 2014 10:07
Swift find function which takes predicate closure
import Foundation
func find<C : Collection>(source: C, includeElement: (C.GeneratorType.Element) -> Bool) -> C.IndexType? {
for i in indices(source) {
if includeElement(source[i]) {
return i;
}
}
@ishikawa
ishikawa / gist:2580
Created July 26, 2008 02:49
domready.js
/**
* domready.js
*
* Cross browser mozilla's 'onDOMContentLoaded' implementation.
* Executes a function when the dom tree is loaded without waiting for images.
*
* Based on +Element.Events.domready+ from Mootools open source project,
* this tiny javascript library adds the emulated 'DOMContentLoaded' functionality.
*
* Features:
@ishikawa
ishikawa / gist:2582
Created July 26, 2008 02:53
Random ASCII Characters
require 'enumerator'
def random_string(length=10, s="")
length.enum_for(:times).inject(s) do |result, index|
s << rand(93) + 33
end
end