webidl2nim

webidl to Nim bindings generator

Pure Nim score 15/100 · tests present · no docs generated

Summary

Latest Version 0.0.1
License MIT
CI Status Failing
Downloads 0
Last Indexed 2026-07-21 05:26

Authors

  • ASVIEST

Installation

nimble install webidl2nim
choosenim install webidl2nim
git clone https://github.com/ASVIEST/webidl2nim

OS Compatibility

Platform Linux macOS Windows FreeBSD OpenBSD NetBSD Android iOS WASM Embedded
webidl2nim - - - - - - -

Dependencies

Package Version Optional
nim >= 1.9.1 No
regex - No
npeg - No
cligen - No

Source

Repository https://github.com/ASVIEST/webidl2nim
Homepage https://github.com/ASVIEST/webidl2nim
Registry Source nimble_official

README

webidl2nim

Tool to translate webidl code to Nim (js target).

Cli

After installing you can just type:

webidl2nim

and translate code via cli cli animation

Quickstart

import 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 JsRoot

User defined types

webidl2nim support to adding new user types.

translateTypesDsl MyMapping:
  HTML5Canvas:
      import pkg/html5_canvas
      -> Canvas
translator.addMapping MyMapping

Features

import std lib modules that needed for definition.

interface NumberWrapper {
  attribute bigint num;
};

Output:

import
  std / jsbigints

type
  NumberWrapper* = ref object of JsRoot
    num* {.importc: "num".}: JsBigInt

automatically reorder code.

interface 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(#)".}

method call syntax support (UFCS)

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.}

Unsupported things:

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.