From e0c0dce2d3557e14c8d7c86ce192eb4746687c8e Mon Sep 17 00:00:00 2001 From: Alexis Glass Date: Sat, 18 Nov 2023 14:07:14 +0100 Subject: [PATCH] Add minimal mention of Wasm/TS support in docs --- README.md | 3 +++ docs/cli-usage.md | 8 +++++++ src/main/scala/djinni/TsGenerator.scala | 4 ++-- src/main/scala/djinni/WasmGenerator.scala | 28 +++++++++++------------ src/main/scala/djinni/YamlGenerator.scala | 2 +- src/main/scala/djinni/parser.scala | 2 +- 6 files changed, 29 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index abe2e3c6..e995dce3 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,7 @@ Djinni generator parses an interface definition file and generates: - Objective-C++ code to convert between C++ and Objective-C - Python and C code to convert between C++ and Python over CFFI - C++/CLI code to convert between C++ and C# +- C++ code to convert between WebAssembly and TS/JS ## Installation @@ -86,3 +87,5 @@ The code in this repository is in large portions copied from [dropbox/djinni](ht - Jacob Potter - Iulia Tamas - Andrew Twyman + +WebAssembly support is borrowed in large part from [snapchat/djinni](https://github.com/snapchat/djinni). \ No newline at end of file diff --git a/docs/cli-usage.md b/docs/cli-usage.md index b05d5bbf..3c27b842 100644 --- a/docs/cli-usage.md +++ b/docs/cli-usage.md @@ -145,6 +145,14 @@ djinni \ | `--cppcli-namespace ...` | The namespace name to use for generated C++/CLI classes. | | `--cppcli-include-cpp-prefix ` | The prefix for `#include` of the main C++ header files from C++/CLI files. | +### WebAssembly/TS/JS + +| Argument | Description | +|----------------------------------------|----------------------------------------------------------------------------| +| `--wasm-out ` | WebAssembly bridge code output folder | +| `--ts-out ` | Path to the Typescript type definitions output folder | +| `--ts-module ` | Name of the module for the Typescript types. `module.ts` by default. | + ### Yaml Generation diff --git a/src/main/scala/djinni/TsGenerator.scala b/src/main/scala/djinni/TsGenerator.scala index 249e6557..b57f7539 100644 --- a/src/main/scala/djinni/TsGenerator.scala +++ b/src/main/scala/djinni/TsGenerator.scala @@ -156,7 +156,7 @@ class TsGenerator(spec: Spec) extends Generator(spec) { } } - //------------------------------------------------------------------------ + // ------------------------------------------------------------------------ private def generateEnum( origin: String, ident: Ident, @@ -245,7 +245,7 @@ class TsGenerator(spec: Spec) extends Generator(spec) { ) + sep + name case None => name } - //-------------------------------------------------------------------------- + // -------------------------------------------------------------------------- override def generate(idl: Seq[TypeDecl]) { createFile( spec.tsOutFolder.get, diff --git a/src/main/scala/djinni/WasmGenerator.scala b/src/main/scala/djinni/WasmGenerator.scala index 12caa6e9..e21df3f9 100644 --- a/src/main/scala/djinni/WasmGenerator.scala +++ b/src/main/scala/djinni/WasmGenerator.scala @@ -234,7 +234,7 @@ class WasmGenerator(spec: Spec) extends Generator(spec) { if (!spec.wasmOmitNsAlias && !spec.wasmNamespace.isEmpty) { w.wl( s""" ::djinni::djinni_register_name_in_ns("${fullyQualifiedName}", "${spec.wasmNamespace.get}.${idJs - .ty(ident)}");""" + .ty(ident)}");""" ) } w.wl(s"});") @@ -249,7 +249,7 @@ class WasmGenerator(spec: Spec) extends Generator(spec) { } } - //------------------------------------------------------------------------------ + // ------------------------------------------------------------------------------ override def generateEnum(origin: String, ident: Ident, doc: Doc, e: Enum) { val refs = new WasmRefs(ident.name) @@ -306,7 +306,7 @@ class WasmGenerator(spec: Spec) extends Generator(spec) { if (!spec.wasmOmitNsAlias && !spec.wasmNamespace.isEmpty) { w.wl( s""" ::djinni::djinni_register_name_in_ns("${fullyQualifiedName}", "${spec.wasmNamespace.get}.${idJs - .ty(ident)}");""" + .ty(ident)}");""" ) } w.wl(s"});") @@ -484,7 +484,7 @@ class WasmGenerator(spec: Spec) extends Generator(spec) { w.wl(";") m.ret.fold(())(r => w.wl(s"return ${helperClass(r.resolved)}::fromCpp(${cppMarshal - .maybeMove("r", r)});") + .maybeMove("r", r)});") ) } w.w("catch(const std::exception& e)").braced { @@ -506,7 +506,7 @@ class WasmGenerator(spec: Spec) extends Generator(spec) { val constModifier = if (m.const) " const" else "" w.w( s"${cppMarshal.fqReturnType(m.ret)} ${helper}::JsProxy::${idCpp - .method(m.ident)}(" + .method(m.ident)}(" ) w.w( m.params @@ -526,7 +526,7 @@ class WasmGenerator(spec: Spec) extends Generator(spec) { s")", p => { s"${helperClass(p.ty.resolved)}::fromCpp(${cppMarshal - .maybeMove(idCpp.local(p.ident), p.ty)})" + .maybeMove(idCpp.local(p.ident), p.ty)})" } ) w.wl(";") @@ -556,7 +556,7 @@ class WasmGenerator(spec: Spec) extends Generator(spec) { val classRegister = if (!spec.wasmOmitNsAlias && !spec.wasmNamespace.isEmpty) { s"""::djinni::DjinniClass_<$cls>("${fullyQualifiedJsName}", "${spec.wasmNamespace.get}.${idJs - .ty(ident.name)}")""" + .ty(ident.name)}")""" } else { s"""em::class_<$cls>("${fullyQualifiedJsName}")""" } @@ -566,13 +566,13 @@ class WasmGenerator(spec: Spec) extends Generator(spec) { s""".smart_ptr>("${fullyQualifiedJsName}")""" ) w.wl(s""".function("${idJs - .method("native_destroy")}", &$helper::nativeDestroy)""") + .method("native_destroy")}", &$helper::nativeDestroy)""") if (i.ext.cpp) { for (m <- i.methods.filter(m => !m.static || m.lang.js)) { val funcType = if (m.static) "class_function" else "function" w.wl(s""".$funcType("${idJs.method( - m.ident.name - )}", $helper::${idCpp.method(m.ident)})""") + m.ident.name + )}", $helper::${idCpp.method(m.ident)})""") } } w.wl(";") @@ -661,7 +661,7 @@ class WasmGenerator(spec: Spec) extends Generator(spec) { "}", f => { s"""${helperClass(f.ty.resolved)}::Boxed::toCpp(j["${idJs - .field(f.ident.name)}"])""" + .field(f.ident.name)}"])""" } ) w.wl(";") @@ -670,9 +670,9 @@ class WasmGenerator(spec: Spec) extends Generator(spec) { w.wl("em::val js = em::val::object();") for (f <- r.fields) { w.wl(s"""js.set("${idJs.field(f.ident.name)}", ${helperClass( - f.ty.resolved - )}::Boxed::fromCpp(${cppMarshal - .maybeMove("c." + idCpp.field(f.ident), f.ty)}));""") + f.ty.resolved + )}::Boxed::fromCpp(${cppMarshal + .maybeMove("c." + idCpp.field(f.ident), f.ty)}));""") } w.wl("return js;") } diff --git a/src/main/scala/djinni/YamlGenerator.scala b/src/main/scala/djinni/YamlGenerator.scala index 8512f825..fe6262a6 100644 --- a/src/main/scala/djinni/YamlGenerator.scala +++ b/src/main/scala/djinni/YamlGenerator.scala @@ -206,7 +206,7 @@ class YamlGenerator(spec: Spec) extends Generator(spec) { private def ts(td: TypeDecl) = Map[String, Any]( "typename" -> tsMarshal.toTsType(mexpr(td), /*addNullability*/ false), "module" -> QuotedString("./" + spec.tsModule) - //, "generic" -> false + // , "generic" -> false ) // TODO: there has to be a way to do all this without the MExpr/Meta conversions? diff --git a/src/main/scala/djinni/parser.scala b/src/main/scala/djinni/parser.scala index d0cdbc06..73d63338 100644 --- a/src/main/scala/djinni/parser.scala +++ b/src/main/scala/djinni/parser.scala @@ -91,7 +91,7 @@ case class Parser(includePaths: List[String]) { InternTypeDecl(ident, typeParams, body, doc, origin) } - def ext(default: Ext): Parser[Ext] = + def ext(default: Ext): Parser[Ext] = (rep1("+" ~> ident) >> checkExts) | success(default) def extRecord: Parser[Ext] = ext( Ext(