Skip to content

Instantly share code, notes, and snippets.

View mjendrusch's full-sized avatar

Michael Jendrusch mjendrusch

View GitHub Profile
@mjendrusch
mjendrusch / JsObject.nim
Created October 21, 2016 00:17
Current state of type-checked JsObject using a compile-time heap.
import macros
import strutils
import typetraits
import tables
import strutils
import sequtils
import compileTimeHeap
{.experimental.}
@mjendrusch
mjendrusch / compileTimeFields.nim
Last active October 20, 2016 17:42
Compile-time fields for Nim
#
# Compile Time Fields
# This module is provided under
#
# The MIT License (MIT)
#
# Copyright (c) 2016 Michael Jendrusch
#
# Permission is hereby granted, free of charge, to any
# person obtaining a copy of this software and associated
@mjendrusch
mjendrusch / jsObj.nim
Created October 13, 2016 21:27
JsObject type for quick and dirty interfacing to JavaScript, with partial type inference
# A fully dynamic type for ad hoc interfacing with JavaScript
import macros
import tables
import typeinfo
import typetraits
{. experimental .}
# Hack for type inference:
# Build a Table of (JsObject instance to (field to type)),
# which updates at each evaluation of `.=`.
@mjendrusch
mjendrusch / bindMethodMacro.nim
Created October 12, 2016 20:36
Macro to bind proc to JavaScript method
import macros
# Macro to build a lambda using JavaScript's `this`
# from a proc, `this` being the first argument.
macro bindMethod*(procedure: typed): auto =
var
rawProc = getImpl(procedure.symbol)
args = rawProc[3]
thisType = args[1][1]
params = newNimNode(nnkFormalParams).add(args[0])
@mjendrusch
mjendrusch / typeSectionMacro.nim
Last active October 11, 2016 21:38
Idea for the exportc everywhere macro
import macros
# Takes a pragma identifier, together with a typesection
macro pragmaTypeSection*(prag, x : untyped): untyped =
result = newNimNode(nnkTypeSection)
for child in x[0].children:
var
name = child[0]
rawBody = child[2]
body = rawBody