Skip to content

ASVIEST/webidl2nim

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

71 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

Tool to translate webidl code to Nim.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages