Tool to translate webidl code to Nim (js target).
After installing you can just type:
webidl2nimimport webidl2nim
import std/[deques, sequtils, sugar]
import pkg/npeg
let t = tokenize"""
interface Hello-Webidl {
};
"""
let c = parseCode(t).stack.toSeq
let translator {.used.} = Translator(
settings: TranslatorSettings(
optionalAttributePolicy: GenDeferredProcs,#UseDefaultVal,#GenDeferredProcs,
features: {
MethodCallSyntax,
NamespaceJsFieldBinding,
ObjConstrRequired,
ReadonlyAttributes
},
exportCode: true,
onIdent: (node: NimUNode, isDecl: bool) =>
node
.nep1Rename(isDecl)
.keywordToAccQuoted()
.makePublic
),
)
import "$nim"/compiler/[ast, renderer]
echo translator.translate(c).assemble(translator.imports).to(PNode)Output:
type
HelloWebidl* = ref object of JsRootwebidl2nim support to adding new user types.
translateTypesDsl MyMapping:
HTML5Canvas:
import pkg/html5_canvas
-> Canvas
translator.addMapping MyMappinginterface NumberWrapper {
attribute bigint num;
};Output:
import
std / jsbigints
type
NumberWrapper* = ref object of JsRoot
num* {.importc: "num".}: JsBigIntinterface NumberWrapper {
type-from-future sum(short ...num);
};
typedef unsigned long type-from-future;Output:
type
TypeFromFuture* = distinct uint32
NumberWrapper* = ref object of JsRoot
proc sum*(self: NumberWrapper; num: varargs[int16]): TypeFromFuture
{.importjs: "#.$1(#)".}interface NumberWrapper {
unsigned long long sum(short ...num);
};Output (with method call syntax):
type
NumberWrapper* = ref object of JsRoot
proc sum*(self: NumberWrapper; num: varargs[int16]): uint64
{.importjs: "#.$1(#)".}Output (without method call syntax):
type
NumberWrapper* = ref object of JsRoot
proc sum*(self: NumberWrapper; num: varargs[int16]): uint64 {.importc.}In webidl when value out of bounds of limited size types, value casting to type.
[Exposed=Window]
interface GraphicsContext {
undefined setColor(octet red, octet green, octet blue);
};var context = getGraphicsContext();
context.setColor(-1, 255, 257); // it's equals to context.setColor(255, 255, 1)It removes the benefits of static typing, so it unsupported.