Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[tsgen] Expand TS generation test to compile and run a TS file. #22582

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions test/other/embind_tsgen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,19 @@ struct ValArr {
EMSCRIPTEN_DECLARE_VAL_TYPE(CallbackType);

struct ValObj {
Foo foo;
Bar bar;
std::string string;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed this to be a more realistic use of a ValueObject.

CallbackType callback;
ValObj() : callback(val::undefined()) {}
};

ValObj getValObj() {
ValObj o;
return o;
}

void setValObj(ValObj v) {}

class ClassWithConstructor {
public:
ClassWithConstructor(int, const ValArr&) {}
Expand Down Expand Up @@ -190,9 +197,11 @@ EMSCRIPTEN_BINDINGS(Test) {
.element(emscripten::index<3>());

value_object<ValObj>("ValObj")
.field("foo", &ValObj::foo)
.field("string", &ValObj::string)
.field("bar", &ValObj::bar)
.field("callback", &ValObj::callback);
function("getValObj", &getValObj);
function("setValObj", &setValObj);

register_vector<int>("IntVec");

Expand Down
14 changes: 8 additions & 6 deletions test/other/embind_tsgen.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,6 @@ export interface ClassWithSmartPtrConstructor {
delete(): void;
}

export type ValObj = {
foo: Foo,
bar: Bar,
callback: (message: string) => void
};

export interface BaseClass {
fn(_0: number): number;
delete(): void;
Expand All @@ -113,6 +107,12 @@ export interface InterfaceWrapper extends Interface {

export type ValArr = [ number, number, number ];

export type ValObj = {
string: EmbindString,
bar: Bar,
callback: (message: string) => void
};

interface EmbindModule {
Test: {
staticFunction(_0: number): number;
Expand Down Expand Up @@ -163,6 +163,8 @@ interface EmbindModule {
smart_ptr_function(_0: ClassWithSmartPtrConstructor | null): number;
smart_ptr_function_with_params(foo: ClassWithSmartPtrConstructor | null): number;
function_with_callback_param(_0: (message: string) => void): number;
getValObj(): ValObj;
setValObj(_0: ValObj): void;
string_test(_0: EmbindString): string;
wstring_test(_0: string): string;
}
Expand Down
28 changes: 28 additions & 0 deletions test/other/embind_tsgen.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

brendandahl marked this conversation as resolved.
Show resolved Hide resolved
import moduleLoader from './embind_tsgen.mjs';
brendandahl marked this conversation as resolved.
Show resolved Hide resolved

const module = await moduleLoader();

// Test a few variations of passing value_objects with strings.
module.setValObj({
bar: module.Bar.valueOne,
string: "ABCD",
callback: () => {}
});

module.setValObj({
bar: module.Bar.valueOne,
string: new Int8Array([65, 66, 67, 68]),
callback: () => {}
});

const valObj = module.getValObj();
// TODO: remove the cast below when better definitions are generated for value
// objects.
const valString : string = valObj.string as string;

// Ensure nonnull pointers do no need a cast or nullptr check to use.
const obj = module.getNonnullPointer();
obj.delete();

console.log('ts ran');
14 changes: 8 additions & 6 deletions test/other/embind_tsgen_ignore_1.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,6 @@ export interface ClassWithSmartPtrConstructor {
delete(): void;
}

export type ValObj = {
foo: Foo,
bar: Bar,
callback: (message: string) => void
};

export interface BaseClass {
fn(_0: number): number;
delete(): void;
Expand All @@ -122,6 +116,12 @@ export interface InterfaceWrapper extends Interface {

export type ValArr = [ number, number, number ];

export type ValObj = {
string: EmbindString,
bar: Bar,
callback: (message: string) => void
};

interface EmbindModule {
Test: {
staticFunction(_0: number): number;
Expand Down Expand Up @@ -172,6 +172,8 @@ interface EmbindModule {
smart_ptr_function(_0: ClassWithSmartPtrConstructor | null): number;
smart_ptr_function_with_params(foo: ClassWithSmartPtrConstructor | null): number;
function_with_callback_param(_0: (message: string) => void): number;
getValObj(): ValObj;
setValObj(_0: ValObj): void;
string_test(_0: EmbindString): string;
wstring_test(_0: string): string;
}
Expand Down
14 changes: 8 additions & 6 deletions test/other/embind_tsgen_ignore_2.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,6 @@ export interface ClassWithSmartPtrConstructor {
delete(): void;
}

export type ValObj = {
foo: Foo,
bar: Bar,
callback: (message: string) => void
};

export interface BaseClass {
fn(_0: number): number;
delete(): void;
Expand All @@ -99,6 +93,12 @@ export interface InterfaceWrapper extends Interface {

export type ValArr = [ number, number, number ];

export type ValObj = {
string: EmbindString,
bar: Bar,
callback: (message: string) => void
};

interface EmbindModule {
Test: {
staticFunction(_0: number): number;
Expand Down Expand Up @@ -149,6 +149,8 @@ interface EmbindModule {
smart_ptr_function(_0: ClassWithSmartPtrConstructor | null): number;
smart_ptr_function_with_params(foo: ClassWithSmartPtrConstructor | null): number;
function_with_callback_param(_0: (message: string) => void): number;
getValObj(): ValObj;
setValObj(_0: ValObj): void;
string_test(_0: EmbindString): string;
wstring_test(_0: string): string;
}
Expand Down
14 changes: 8 additions & 6 deletions test/other/embind_tsgen_ignore_3.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,6 @@ export interface ClassWithSmartPtrConstructor {
delete(): void;
}

export type ValObj = {
foo: Foo,
bar: Bar,
callback: (message: string) => void
};

export interface BaseClass {
fn(_0: number): number;
delete(): void;
Expand All @@ -113,6 +107,12 @@ export interface InterfaceWrapper extends Interface {

export type ValArr = [ number, number, number ];

export type ValObj = {
string: EmbindString,
bar: Bar,
callback: (message: string) => void
};

interface EmbindModule {
Test: {
staticFunction(_0: number): number;
Expand Down Expand Up @@ -163,6 +163,8 @@ interface EmbindModule {
smart_ptr_function(_0: ClassWithSmartPtrConstructor | null): number;
smart_ptr_function_with_params(foo: ClassWithSmartPtrConstructor | null): number;
function_with_callback_param(_0: (message: string) => void): number;
getValObj(): ValObj;
setValObj(_0: ValObj): void;
string_test(_0: EmbindString): string;
wstring_test(_0: string): string;
}
Expand Down
13 changes: 13 additions & 0 deletions test/other/embind_tsgen_package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "index",
"version": "1.0.0",
"description": "",
"main": "main.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": ""
brendandahl marked this conversation as resolved.
Show resolved Hide resolved
}
10 changes: 7 additions & 3 deletions test/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -3365,12 +3365,16 @@ def test_jspi_add_function(self):
def test_embind_tsgen(self, opts):
# Check that TypeScript generation works and that the program is runs as
# expected.
self.do_runf('other/embind_tsgen.cpp', 'main ran',
emcc_args=['-lembind', '--emit-tsd', 'embind_tsgen.d.ts'] + opts)
self.emcc(test_file('other/embind_tsgen.cpp'),
['-o', 'embind_tsgen.mjs', '-lembind', '--emit-tsd', 'embind_tsgen.d.ts'] + opts)

# Test that the output compiles with a TS file that uses the defintions.
cmd = shared.get_npm_cmd('tsc') + ['embind_tsgen.d.ts', '--noEmit']
shutil.copyfile(test_file('other/embind_tsgen.ts'), 'main.ts')
brendandahl marked this conversation as resolved.
Show resolved Hide resolved
# A package file with type=module is needed to allow top level await in TS.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you instead use the .mjs extension?

shutil.copyfile(test_file('other/embind_tsgen_package.json'), 'package.json')
cmd = shared.get_npm_cmd('tsc') + ['embind_tsgen.d.ts', 'main.ts', '--module', 'NodeNext', '--moduleResolution', 'nodenext']
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why doesn't tsc generate a file called .mjs here I wonder?

Is the package.json file here needed for running tsc or for running the output?

shared.check_call(cmd)
self.assertContained('main ran\nts ran', self.run_js('main.js'))

actual = read_file('embind_tsgen.d.ts')
self.assertFileContents(test_file('other/embind_tsgen.d.ts'), actual)
Expand Down
Loading