Skip to content

Commit

Permalink
Bug 1782784 - Convert UniFFI JS bindings to ESM. r=nika
Browse files Browse the repository at this point in the history
  • Loading branch information
mathewhodson committed May 8, 2023
1 parent 3cde859 commit 9840cc5
Show file tree
Hide file tree
Showing 55 changed files with 378 additions and 640 deletions.
10 changes: 5 additions & 5 deletions docs/writing-rust-code/uniffi.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ interoperability.
The [`uniffi-bindgen-gecko-js`](https://searchfox.org/mozilla-central/source/toolkit/components/uniffi-bindgen-gecko-js)
tool, which lives in the Firefox source tree, generates 2 things:
- A JS interface for the scaffolding code, which uses [WebIDL](/dom/bindings/webidl/index.rst)
- A JSM module that uses the scaffolding to provide the bindings API.
- A module that uses the scaffolding to provide the bindings API.

Currently, this generated code gets checked in to source control. We are working on a system to avoid this and
auto-generate it at build time instead (see [bugzilla 1756214](https://bugzilla.mozilla.org/show_bug.cgi?id=1756214)).
Expand Down Expand Up @@ -55,15 +55,15 @@ Here's how you can create a new set of bindings using UniFFI:
3. Generate bindings code for your crate
- Add the path of your UDL (that you made in step 1) in `toolkit/components/uniffi-bindgen-gecko-js/mach_commands.py`
- Run `./mach uniffi generate`
- add your newly generated `Rust{udl-name}.jsm` file to `toolkit/components/uniffi-bindgen-gecko-js/components/moz.build`
- Then simply import your `jsm` module to the file you want to use it in and start using your APIs!
- add your newly generated `Rust{udl-name}.sys.mjs` file to `toolkit/components/uniffi-bindgen-gecko-js/components/moz.build`
- Then simply import your module to the file you want to use it in and start using your APIs!

Example from tabs module:

``` js
XPCOMUtils.defineLazyModuleGetters(lazy, {
ChromeUtils.defineESModuleGetters(lazy, {
...
TabsStore: "resource://gre/modules/RustTabs.jsm",
TabsStore: "resource://gre/modules/RustTabs.sys.mjs",
});
...
this._rustStore = await lazy.TabsStore.init(path);
Expand Down
8 changes: 1 addition & 7 deletions services/sync/modules/engines/tabs.sys.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,16 @@
const STORAGE_VERSION = 1; // This needs to be kept in-sync with the rust storage version

import { XPCOMUtils } from "resource://gre/modules/XPCOMUtils.sys.mjs";

import { SyncEngine, Tracker } from "resource://services-sync/engines.sys.mjs";
import { Svc, Utils } from "resource://services-sync/util.sys.mjs";
import { Log } from "resource://gre/modules/Log.sys.mjs";

import {
SCORE_INCREMENT_SMALL,
STATUS_OK,
URI_LENGTH_MAX,
} from "resource://services-sync/constants.sys.mjs";
import { CommonUtils } from "resource://services-common/utils.sys.mjs";
import { Async } from "resource://services-common/async.sys.mjs";

import {
SyncRecord,
SyncTelemetry,
Expand All @@ -32,10 +29,7 @@ ChromeUtils.defineESModuleGetters(lazy, {
PlacesUtils: "resource://gre/modules/PlacesUtils.sys.mjs",
PrivateBrowsingUtils: "resource://gre/modules/PrivateBrowsingUtils.sys.mjs",
ReaderMode: "resource://gre/modules/ReaderMode.sys.mjs",
});

XPCOMUtils.defineLazyModuleGetters(lazy, {
TabsStore: "resource://gre/modules/RustTabs.jsm",
TabsStore: "resource://gre/modules/RustTabs.sys.mjs",
});

XPCOMUtils.defineLazyPreferenceGetter(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
// This file was autogenerated by the `uniffi-bindgen-gecko-js` crate.
// Trust me, you don't want to mess with it!

const { UniFFITypeError } = ChromeUtils.importESModule("resource://gre/modules/UniFFI.sys.mjs");
import { UniFFITypeError } from "resource://gre/modules/UniFFI.sys.mjs";



"use strict";

// Objects intended to be used in the unit tests
var UnitTestObjs = {};

var EXPORTED_SYMBOLS = ["UnitTestObjs"];
export var UnitTestObjs = {};

// Write/Read data to/from an ArrayBuffer
class ArrayBufferDataStream {
Expand Down Expand Up @@ -265,7 +261,8 @@ const uniffiObjectPtr = Symbol("uniffiObjectPtr");
const constructUniffiObject = Symbol("constructUniffiObject");
UnitTestObjs.uniffiObjectPtr = uniffiObjectPtr;

class FfiConverterI64 extends FfiConverter {
// Export the FFIConverter object to make external types work.
export class FfiConverterI64 extends FfiConverter {
static checkType(value) {
super.checkType(value);
if (!Number.isSafeInteger(value)) {
Expand All @@ -290,9 +287,7 @@ class FfiConverterI64 extends FfiConverter {
}

// Export the FFIConverter object to make external types work.
EXPORTED_SYMBOLS.push("FfiConverterI64");

class FfiConverterString extends FfiConverter {
export class FfiConverterString extends FfiConverter {
static checkType(value) {
super.checkType(value);
if (typeof value !== "string") {
Expand Down Expand Up @@ -324,10 +319,7 @@ class FfiConverterString extends FfiConverter {
}
}

// Export the FFIConverter object to make external types work.
EXPORTED_SYMBOLS.push("FfiConverterString");

class TabsBridgedEngine {
export class TabsBridgedEngine {
// Use `init` to instantiate this class.
// DO NOT USE THIS CONSTRUCTOR DIRECTLY
constructor(opts) {
Expand Down Expand Up @@ -605,7 +597,8 @@ class TabsBridgedEngine {

}

class FfiConverterTypeTabsBridgedEngine extends FfiConverter {
// Export the FFIConverter object to make external types work.
export class FfiConverterTypeTabsBridgedEngine extends FfiConverter {
static lift(value) {
const opts = {};
opts[constructUniffiObject] = value;
Expand All @@ -629,12 +622,7 @@ class FfiConverterTypeTabsBridgedEngine extends FfiConverter {
}
}

EXPORTED_SYMBOLS.push("TabsBridgedEngine");

// Export the FFIConverter object to make external types work.
EXPORTED_SYMBOLS.push("FfiConverterTypeTabsBridgedEngine");

class TabsStore {
export class TabsStore {
// Use `init` to instantiate this class.
// DO NOT USE THIS CONSTRUCTOR DIRECTLY
constructor(opts) {
Expand Down Expand Up @@ -828,7 +816,8 @@ class TabsStore {

}

class FfiConverterTypeTabsStore extends FfiConverter {
// Export the FFIConverter object to make external types work.
export class FfiConverterTypeTabsStore extends FfiConverter {
static lift(value) {
const opts = {};
opts[constructUniffiObject] = value;
Expand All @@ -852,12 +841,7 @@ class FfiConverterTypeTabsStore extends FfiConverter {
}
}

EXPORTED_SYMBOLS.push("TabsStore");

// Export the FFIConverter object to make external types work.
EXPORTED_SYMBOLS.push("FfiConverterTypeTabsStore");

class ClientRemoteTabs {
export class ClientRemoteTabs {
constructor(clientId,clientName,deviceType,lastModified,remoteTabs) {
try {
FfiConverterString.checkType(clientId)
Expand Down Expand Up @@ -916,7 +900,8 @@ class ClientRemoteTabs {
}
}

class FfiConverterTypeClientRemoteTabs extends FfiConverterArrayBuffer {
// Export the FFIConverter object to make external types work.
export class FfiConverterTypeClientRemoteTabs extends FfiConverterArrayBuffer {
static read(dataStream) {
return new ClientRemoteTabs(
FfiConverterString.read(dataStream),
Expand Down Expand Up @@ -989,12 +974,7 @@ class FfiConverterTypeClientRemoteTabs extends FfiConverterArrayBuffer {
}
}

EXPORTED_SYMBOLS.push("ClientRemoteTabs");

// Export the FFIConverter object to make external types work.
EXPORTED_SYMBOLS.push("FfiConverterTypeClientRemoteTabs");

class RemoteTabRecord {
export class RemoteTabRecord {
constructor(title,urlHistory,icon,lastUsed) {
try {
FfiConverterString.checkType(title)
Expand Down Expand Up @@ -1043,7 +1023,8 @@ class RemoteTabRecord {
}
}

class FfiConverterTypeRemoteTabRecord extends FfiConverterArrayBuffer {
// Export the FFIConverter object to make external types work.
export class FfiConverterTypeRemoteTabRecord extends FfiConverterArrayBuffer {
static read(dataStream) {
return new RemoteTabRecord(
FfiConverterString.read(dataStream),
Expand Down Expand Up @@ -1105,12 +1086,7 @@ class FfiConverterTypeRemoteTabRecord extends FfiConverterArrayBuffer {
}
}

EXPORTED_SYMBOLS.push("RemoteTabRecord");

// Export the FFIConverter object to make external types work.
EXPORTED_SYMBOLS.push("FfiConverterTypeRemoteTabRecord");

const TabsDeviceType = {
export const TabsDeviceType = {
DESKTOP: 1,
MOBILE: 2,
TABLET: 3,
Expand All @@ -1120,7 +1096,8 @@ const TabsDeviceType = {
};

Object.freeze(TabsDeviceType);
class FfiConverterTypeTabsDeviceType extends FfiConverterArrayBuffer {
// Export the FFIConverter object to make external types work.
export class FfiConverterTypeTabsDeviceType extends FfiConverterArrayBuffer {
static read(dataStream) {
switch (dataStream.readInt32()) {
case 1:
Expand Down Expand Up @@ -1179,19 +1156,13 @@ class FfiConverterTypeTabsDeviceType extends FfiConverterArrayBuffer {
}
}

EXPORTED_SYMBOLS.push("TabsDeviceType");

// Export the FFIConverter object to make external types work.
EXPORTED_SYMBOLS.push("FfiConverterTypeTabsDeviceType");


export class TabsApiError extends Error {}

class TabsApiError extends Error {}
EXPORTED_SYMBOLS.push("TabsApiError");

export class SyncError extends TabsApiError {

class SyncError extends TabsApiError {

constructor(
reason,
...params
Expand All @@ -1203,9 +1174,9 @@ class SyncError extends TabsApiError {
return `SyncError: ${super.toString()}`
}
}
EXPORTED_SYMBOLS.push("SyncError");
class SqlError extends TabsApiError {

export class SqlError extends TabsApiError {

constructor(
reason,
...params
Expand All @@ -1217,9 +1188,9 @@ class SqlError extends TabsApiError {
return `SqlError: ${super.toString()}`
}
}
EXPORTED_SYMBOLS.push("SqlError");
class UnexpectedTabsError extends TabsApiError {

export class UnexpectedTabsError extends TabsApiError {

constructor(
reason,
...params
Expand All @@ -1231,9 +1202,9 @@ class UnexpectedTabsError extends TabsApiError {
return `UnexpectedTabsError: ${super.toString()}`
}
}
EXPORTED_SYMBOLS.push("UnexpectedTabsError");

class FfiConverterTypeTabsApiError extends FfiConverterArrayBuffer {
// Export the FFIConverter object to make external types work.
export class FfiConverterTypeTabsApiError extends FfiConverterArrayBuffer {
static read(dataStream) {
switch (dataStream.readInt32()) {
case 1:
Expand Down Expand Up @@ -1292,9 +1263,7 @@ class FfiConverterTypeTabsApiError extends FfiConverterArrayBuffer {
}

// Export the FFIConverter object to make external types work.
EXPORTED_SYMBOLS.push("FfiConverterTypeTabsApiError");

class FfiConverterOptionalstring extends FfiConverterArrayBuffer {
export class FfiConverterOptionalstring extends FfiConverterArrayBuffer {
static checkType(value) {
if (value !== undefined && value !== null) {
FfiConverterString.checkType(value)
Expand Down Expand Up @@ -1331,9 +1300,7 @@ class FfiConverterOptionalstring extends FfiConverterArrayBuffer {
}

// Export the FFIConverter object to make external types work.
EXPORTED_SYMBOLS.push("FfiConverterOptionalstring");

class FfiConverterSequencestring extends FfiConverterArrayBuffer {
export class FfiConverterSequencestring extends FfiConverterArrayBuffer {
static read(dataStream) {
const len = dataStream.readInt32();
const arr = [];
Expand Down Expand Up @@ -1377,9 +1344,7 @@ class FfiConverterSequencestring extends FfiConverterArrayBuffer {
}

// Export the FFIConverter object to make external types work.
EXPORTED_SYMBOLS.push("FfiConverterSequencestring");

class FfiConverterSequenceTypeClientRemoteTabs extends FfiConverterArrayBuffer {
export class FfiConverterSequenceTypeClientRemoteTabs extends FfiConverterArrayBuffer {
static read(dataStream) {
const len = dataStream.readInt32();
const arr = [];
Expand Down Expand Up @@ -1423,9 +1388,7 @@ class FfiConverterSequenceTypeClientRemoteTabs extends FfiConverterArrayBuffer {
}

// Export the FFIConverter object to make external types work.
EXPORTED_SYMBOLS.push("FfiConverterSequenceTypeClientRemoteTabs");

class FfiConverterSequenceTypeRemoteTabRecord extends FfiConverterArrayBuffer {
export class FfiConverterSequenceTypeRemoteTabRecord extends FfiConverterArrayBuffer {
static read(dataStream) {
const len = dataStream.readInt32();
const arr = [];
Expand Down Expand Up @@ -1469,9 +1432,7 @@ class FfiConverterSequenceTypeRemoteTabRecord extends FfiConverterArrayBuffer {
}

// Export the FFIConverter object to make external types work.
EXPORTED_SYMBOLS.push("FfiConverterSequenceTypeRemoteTabRecord");

class FfiConverterSequenceTypeTabsGuid extends FfiConverterArrayBuffer {
export class FfiConverterSequenceTypeTabsGuid extends FfiConverterArrayBuffer {
static read(dataStream) {
const len = dataStream.readInt32();
const arr = [];
Expand Down Expand Up @@ -1515,11 +1476,8 @@ class FfiConverterSequenceTypeTabsGuid extends FfiConverterArrayBuffer {
}

// Export the FFIConverter object to make external types work.
EXPORTED_SYMBOLS.push("FfiConverterSequenceTypeTabsGuid");

class FfiConverterTypeTabsGuid extends FfiConverter {

static lift(buf) {
export class FfiConverterTypeTabsGuid extends FfiConverter {
static lift(buf) {
return FfiConverterString.lift(buf);
}

Expand All @@ -1541,9 +1499,6 @@ class FfiConverterTypeTabsGuid extends FfiConverter {
}
// TODO: We should also allow JS to customize the type eventually.

// Export the FFIConverter object to make external types work.
EXPORTED_SYMBOLS.push("FfiConverterTypeTabsGuid");




Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

EXTRA_JS_MODULES += [
"generated/RustTabs.jsm",
"generated/RustTabs.sys.mjs",
]

with Files("**"):
Expand Down
Loading

0 comments on commit 9840cc5

Please sign in to comment.