Skip to content

Commit

Permalink
[wasm] Make typescript and rollup build incremental (#62772)
Browse files Browse the repository at this point in the history
- eliminate slow ArgsMarshalString
- make rollup build incremental
- exclude dotnet.d.ts from typescript inputs
  • Loading branch information
pavelsavara authored Dec 14, 2021
1 parent 6b224f3 commit 842f4d7
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 53 deletions.
3 changes: 1 addition & 2 deletions src/mono/wasm/runtime/corebindings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
// The .NET Foundation licenses this file to you under the MIT license.

import { JSHandle, GCHandle, MonoObject } from "./types";
import { ArgsMarshalString } from "./method-binding";
import { PromiseControl } from "./cancelable-promise";
import { runtimeHelpers } from "./imports";

const fn_signatures: [jsname: string, csname: string, signature: ArgsMarshalString][] = [
const fn_signatures: [jsname: string, csname: string, signature: string/*ArgsMarshalString*/][] = [
["_get_cs_owned_object_by_js_handle", "GetCSOwnedObjectByJSHandle", "ii!"],
["_get_cs_owned_object_js_handle", "GetCSOwnedObjectJSHandle", "mi"],
["_try_get_cs_owned_object_js_handle", "TryGetCSOwnedObjectJSHandle", "mi"],
Expand Down
19 changes: 2 additions & 17 deletions src/mono/wasm/runtime/dotnet.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,21 +114,6 @@ declare class WasmRoot<T extends ManagedPointer | NativePointer> {
toString(): string;
}

declare const enum ArgsMarshal {
Int32 = "i",
Int32Enum = "j",
Int64 = "l",
Int64Enum = "k",
Float32 = "f",
Float64 = "d",
String = "s",
Char = "s",
JSObj = "o",
MONOObj = "m"
}
declare type _ExtraArgsMarshalOperators = "!" | "";
declare type ArgsMarshalString = "" | `${ArgsMarshal}${_ExtraArgsMarshalOperators}` | `${ArgsMarshal}${ArgsMarshal}${_ExtraArgsMarshalOperators}` | `${ArgsMarshal}${ArgsMarshal}${ArgsMarshal}${_ExtraArgsMarshalOperators}` | `${ArgsMarshal}${ArgsMarshal}${ArgsMarshal}${ArgsMarshal}${_ExtraArgsMarshalOperators}`;

interface MonoObject extends ManagedPointer {
__brandMonoObject: "MonoObject";
}
Expand Down Expand Up @@ -260,8 +245,8 @@ declare function js_typed_array_to_array(js_obj: any): MonoArray;
declare function unbox_mono_obj(mono_obj: MonoObject): any;
declare function mono_array_to_js_array(mono_array: MonoArray): any[] | null;

declare function mono_bind_static_method(fqn: string, signature?: ArgsMarshalString): Function;
declare function mono_call_assembly_entry_point(assembly: string, args?: any[], signature?: ArgsMarshalString): number;
declare function mono_bind_static_method(fqn: string, signature?: string): Function;
declare function mono_call_assembly_entry_point(assembly: string, args?: any[], signature?: string): number;

declare function mono_wasm_load_bytes_into_heap(bytes: Uint8Array): VoidPtr;

Expand Down
3 changes: 1 addition & 2 deletions src/mono/wasm/runtime/exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ import { mono_wasm_add_event_listener, mono_wasm_remove_event_listener } from ".
import { mono_wasm_release_cs_owned_object } from "./gc-handles";
import { mono_wasm_web_socket_open, mono_wasm_web_socket_send, mono_wasm_web_socket_receive, mono_wasm_web_socket_close, mono_wasm_web_socket_abort } from "./web-socket";
import cwraps from "./cwraps";
import { ArgsMarshalString } from "./method-binding";
import {
setI8, setI16, setI32, setI64,
setU8, setU16, setU32, setF32, setF64,
Expand Down Expand Up @@ -219,7 +218,7 @@ function initializeImportsAndExports(
// backward compatibility
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
module.mono_bind_static_method = (fqn: string, signature: ArgsMarshalString): Function => {
module.mono_bind_static_method = (fqn: string, signature: string/*ArgsMarshalString*/): Function => {
console.warn("Module.mono_bind_static_method is obsolete, please use BINDING.bind_static_method instead");
return mono_bind_static_method(fqn, signature);
};
Expand Down
20 changes: 10 additions & 10 deletions src/mono/wasm/runtime/method-binding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export function get_method(method_name: string): MonoMethod {
return res;
}

export function bind_runtime_method(method_name: string, signature: ArgsMarshalString): Function {
export function bind_runtime_method(method_name: string, signature: string): Function {
const method = get_method(method_name);
return mono_bind_method(method, null, signature, "BINDINGS_" + method_name);
}
Expand Down Expand Up @@ -139,7 +139,7 @@ export function _create_primitive_converters(): void {
result.set("d", { steps: [{ indirect: "double" }], size: 8 });
}

function _create_converter_for_marshal_string(args_marshal: ArgsMarshalString): Converter {
function _create_converter_for_marshal_string(args_marshal: string/*ArgsMarshalString*/): Converter {
const steps = [];
let size = 0;
let is_result_definitely_unmarshaled = false,
Expand Down Expand Up @@ -184,7 +184,7 @@ function _create_converter_for_marshal_string(args_marshal: ArgsMarshalString):
};
}

function _get_converter_for_marshal_string(args_marshal: ArgsMarshalString): Converter {
function _get_converter_for_marshal_string(args_marshal: string/*ArgsMarshalString*/): Converter {
let converter = _signature_converters.get(args_marshal);
if (!converter) {
converter = _create_converter_for_marshal_string(args_marshal);
Expand All @@ -194,7 +194,7 @@ function _get_converter_for_marshal_string(args_marshal: ArgsMarshalString): Con
return converter;
}

export function _compile_converter_for_marshal_string(args_marshal: ArgsMarshalString): Converter {
export function _compile_converter_for_marshal_string(args_marshal: string/*ArgsMarshalString*/): Converter {
const converter = _get_converter_for_marshal_string(args_marshal);
if (typeof (converter.args_marshal) !== "string")
throw new Error("Corrupt converter for '" + args_marshal + "'");
Expand Down Expand Up @@ -372,7 +372,7 @@ export function _decide_if_result_is_marshaled(converter: Converter, argc: numbe
}
}

export function mono_bind_method(method: MonoMethod, this_arg: MonoObject | null, args_marshal: ArgsMarshalString, friendly_name: string): Function {
export function mono_bind_method(method: MonoMethod, this_arg: MonoObject | null, args_marshal: string/*ArgsMarshalString*/, friendly_name: string): Function {
if (typeof (args_marshal) !== "string")
throw new Error("args_marshal argument invalid, expected string");
this_arg = coerceNull(this_arg);
Expand Down Expand Up @@ -546,6 +546,9 @@ export function mono_bind_method(method: MonoMethod, this_arg: MonoObject | null
return result;
}

/*
We currently don't use these types because it makes typeScript compiler very slow.
declare const enum ArgsMarshal {
Int32 = "i", // int32
Int32Enum = "j", // int32 - Enum with underlying type of int32
Expand All @@ -562,15 +565,12 @@ declare const enum ArgsMarshal {
// to suppress marshaling of the return value, place '!' at the end of args_marshal, i.e. 'ii!' instead of 'ii'
type _ExtraArgsMarshalOperators = "!" | "";
// TODO make this more efficient so we can add more parameters (currently it only checks up to 4). One option is to add a
// blank to the ArgsMarshal enum but that doesn't solve the TS limit of number of options in 1 type
// Take the marshaling enums and convert to all the valid strings for type checking.
export type ArgsMarshalString = ""
| `${ArgsMarshal}${_ExtraArgsMarshalOperators}`
| `${ArgsMarshal}${ArgsMarshal}${_ExtraArgsMarshalOperators}`
| `${ArgsMarshal}${ArgsMarshal}${ArgsMarshal}${_ExtraArgsMarshalOperators}`
| `${ArgsMarshal}${ArgsMarshal}${ArgsMarshal}${ArgsMarshal}${_ExtraArgsMarshalOperators}`;

*/

type ConverterStepIndirects = "u32" | "i32" | "float" | "double" | "i64"

Expand All @@ -583,7 +583,7 @@ export type Converter = {
size?: number;
}[];
size: number;
args_marshal?: ArgsMarshalString;
args_marshal?: string/*ArgsMarshalString*/;
is_result_definitely_unmarshaled?: boolean;
is_result_possibly_unmarshaled?: boolean;
result_unmarshaled_if_argc?: number;
Expand Down
18 changes: 9 additions & 9 deletions src/mono/wasm/runtime/method-calls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { _mono_array_root_to_js_array, _unbox_mono_obj_root } from "./cs-to-js";
import { get_js_obj, mono_wasm_get_jsobj_from_js_handle } from "./gc-handles";
import { js_array_to_mono_array, _box_js_bool, _js_to_mono_obj } from "./js-to-cs";
import {
ArgsMarshalString, mono_bind_method,
mono_bind_method,
Converter, _compile_converter_for_marshal_string,
_decide_if_result_is_marshaled, find_method,
BoundMethodToken
Expand All @@ -23,7 +23,7 @@ import { bindings_lazy_init } from "./startup";
import { _create_temp_frame, _release_temp_frame } from "./memory";
import { VoidPtr, Int32Ptr, EmscriptenModule } from "./types/emscripten";

function _verify_args_for_method_call(args_marshal: ArgsMarshalString, args: any) {
function _verify_args_for_method_call(args_marshal: string/*ArgsMarshalString*/, args: any) {
const has_args = args && (typeof args === "object") && args.length > 0;
const has_args_marshal = typeof args_marshal === "string";

Expand Down Expand Up @@ -140,7 +140,7 @@ m: raw mono object. Don't use it unless you know what you're doing
to suppress marshaling of the return value, place '!' at the end of args_marshal, i.e. 'ii!' instead of 'ii'
*/
export function call_method(method: MonoMethod, this_arg: MonoObject | undefined, args_marshal: ArgsMarshalString, args: ArrayLike<any>): any {
export function call_method(method: MonoMethod, this_arg: MonoObject | undefined, args_marshal: string/*ArgsMarshalString*/, args: ArrayLike<any>): any {
// HACK: Sometimes callers pass null or undefined, coerce it to 0 since that's what wasm expects
this_arg = coerceNull(this_arg);

Expand Down Expand Up @@ -239,7 +239,7 @@ function _call_method_with_converted_args(
return _handle_exception_and_produce_result_for_call(converter, token, buffer, resultRoot, exceptionRoot, argsRootBuffer, is_result_marshaled);
}

export function call_static_method(fqn: string, args: any[], signature: ArgsMarshalString): any {
export function call_static_method(fqn: string, args: any[], signature: string/*ArgsMarshalString*/): any {
bindings_lazy_init();// TODO remove this once Blazor does better startup

const method = mono_method_resolve(fqn);
Expand All @@ -250,18 +250,18 @@ export function call_static_method(fqn: string, args: any[], signature: ArgsMars
return call_method(method, undefined, signature, args);
}

export function mono_bind_static_method(fqn: string, signature?: ArgsMarshalString): Function {
export function mono_bind_static_method(fqn: string, signature?: string/*ArgsMarshalString*/): Function {
bindings_lazy_init();// TODO remove this once Blazor does better startup

const method = mono_method_resolve(fqn);

if (typeof signature === "undefined")
signature = mono_method_get_call_signature(method);

return mono_bind_method(method, null, signature, fqn);
return mono_bind_method(method, null, signature!, fqn);
}

export function mono_bind_assembly_entry_point(assembly: string, signature?: ArgsMarshalString): Function {
export function mono_bind_assembly_entry_point(assembly: string, signature?: string/*ArgsMarshalString*/): Function {
bindings_lazy_init();// TODO remove this once Blazor does better startup

const asm = cwraps.mono_wasm_assembly_load(assembly);
Expand All @@ -282,7 +282,7 @@ export function mono_bind_assembly_entry_point(assembly: string, signature?: Arg
};
}

export function mono_call_assembly_entry_point(assembly: string, args?: any[], signature?: ArgsMarshalString): number {
export function mono_call_assembly_entry_point(assembly: string, args?: any[], signature?: string/*ArgsMarshalString*/): number {
if (!args) {
args = [[]];
}
Expand Down Expand Up @@ -471,7 +471,7 @@ export function wrap_error(is_exception: Int32Ptr | null, ex: any): MonoString {
return js_string_to_mono_string(res)!;
}

export function mono_method_get_call_signature(method: MonoMethod, mono_obj?: MonoObject): ArgsMarshalString {
export function mono_method_get_call_signature(method: MonoMethod, mono_obj?: MonoObject): string/*ArgsMarshalString*/ {
const instanceRoot = mono_wasm_new_root(mono_obj);
try {
return call_method(runtimeHelpers.get_call_sig, undefined, "im", [method, instanceRoot.value]);
Expand Down
1 change: 1 addition & 0 deletions src/mono/wasm/runtime/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"outDir": "bin",
},
"exclude": [
"dotnet.d.ts",
"bin"
]
}
48 changes: 35 additions & 13 deletions src/mono/wasm/wasm.proj
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@
<!-- This is a documented target that is invoked by developers in their innerloop work. -->
<Target Name="BuildWasmRuntimes"
AfterTargets="Build"
DependsOnTargets="GenerateEmccPropsAndRspFiles;BuildPInvokeTable;BundleTimeZones">
DependsOnTargets="GenerateEmccPropsAndRspFiles;BuildPInvokeTable;BundleTimeZones;InstallNpmPackages;BuildWithRollup">

<ItemGroup>
<ICULibNativeFiles Include="$(ICULibDir)/libicuuc.a;
Expand All @@ -167,18 +167,6 @@
DestinationFolder="$(MonoObjDir)"
SkipUnchangedFiles="true" />

<!-- install typescript and rollup -->
<RunWithEmSdkEnv Condition="'$(ContinuousIntegrationBuild)' == 'true'" Command="npm ci" EmSdkPath="$(EMSDK_PATH)" IgnoreStandardErrorWarningFormat="true" WorkingDirectory="$(MonoProjectRoot)wasm/runtime/"/>
<RunWithEmSdkEnv Condition="'$(ContinuousIntegrationBuild)' == 'true'" Command="npm audit" EmSdkPath="$(EMSDK_PATH)" IgnoreStandardErrorWarningFormat="true" WorkingDirectory="$(MonoProjectRoot)wasm/runtime/"/>
<!-- npm install is faster on dev machine as it doesn't wipe node_modules folder -->
<RunWithEmSdkEnv Condition="'$(ContinuousIntegrationBuild)' != 'true'" Command="npm install" EmSdkPath="$(EMSDK_PATH)" IgnoreStandardErrorWarningFormat="true" WorkingDirectory="$(MonoProjectRoot)wasm/runtime/"/>

<!-- code style check -->
<RunWithEmSdkEnv Command="npm run lint" StandardOutputImportance="High" EmSdkPath="$(EMSDK_PATH)" WorkingDirectory="$(MonoProjectRoot)wasm/runtime/"/>

<!-- compile typescript -->
<RunWithEmSdkEnv Command="npm run rollup -- --environment Configuration:$(Configuration),NativeBinDir:$(NativeBinDir),ProductVersion:$(ProductVersion)" EmSdkPath="$(EMSDK_PATH)" IgnoreStandardErrorWarningFormat="true" WorkingDirectory="$(MonoProjectRoot)wasm/runtime/"/>

<Copy SourceFiles="runtime/driver.c;
runtime/pinvoke.c;
runtime/corebindings.c;
Expand Down Expand Up @@ -252,4 +240,38 @@
SkipUnchangedFiles="true" />
</Target>

<Target Name="InstallNpmPackages"
Inputs="$(MonoProjectRoot)wasm/runtime/package.json"
Outputs="$(MonoProjectRoot)wasm/runtime/node_modules/.npm-stamp"
>
<!-- install typescript and rollup -->
<RunWithEmSdkEnv Condition="'$(ContinuousIntegrationBuild)' == 'true'" Command="npm ci" EmSdkPath="$(EMSDK_PATH)" IgnoreStandardErrorWarningFormat="true" WorkingDirectory="$(MonoProjectRoot)wasm/runtime/"/>
<RunWithEmSdkEnv Condition="'$(ContinuousIntegrationBuild)' == 'true'" Command="npm audit" EmSdkPath="$(EMSDK_PATH)" IgnoreStandardErrorWarningFormat="true" WorkingDirectory="$(MonoProjectRoot)wasm/runtime/"/>
<!-- npm install is faster on dev machine as it doesn't wipe node_modules folder -->
<RunWithEmSdkEnv Condition="'$(ContinuousIntegrationBuild)' != 'true'" Command="npm install" EmSdkPath="$(EMSDK_PATH)" IgnoreStandardErrorWarningFormat="true" WorkingDirectory="$(MonoProjectRoot)wasm/runtime/"/>

<Touch Files="$(MonoProjectRoot)wasm/runtime/node_modules/.npm-stamp" AlwaysCreate="true" />
</Target>


<ItemGroup>
<_RollupInputs Include="$(MonoProjectRoot)wasm/runtime/*.ts"/>
<_RollupInputs Include="$(MonoProjectRoot)wasm/runtime/types/*.ts"/>
<_RollupInputs Include="$(MonoProjectRoot)wasm/runtimetypes/*.d.ts"/>
<_RollupInputs Include="$(MonoProjectRoot)wasm/runtime/*.json"/>
<_RollupInputs Include="$(MonoProjectRoot)wasm/runtime/*.js"/>
</ItemGroup>

<Target Name="BuildWithRollup"
Inputs="@(_RollupInputs)"
Outputs="$(NativeBinDir).rollup-stamp"
>
<!-- code style check -->
<RunWithEmSdkEnv Command="npm run lint" StandardOutputImportance="High" EmSdkPath="$(EMSDK_PATH)" WorkingDirectory="$(MonoProjectRoot)wasm/runtime/"/>

<!-- compile typescript -->
<RunWithEmSdkEnv Command="npm run rollup -- --environment Configuration:$(Configuration),NativeBinDir:$(NativeBinDir),ProductVersion:$(ProductVersion)" EmSdkPath="$(EMSDK_PATH)" IgnoreStandardErrorWarningFormat="true" WorkingDirectory="$(MonoProjectRoot)wasm/runtime/"/>

<Touch Files="$(NativeBinDir).rollup-stamp" AlwaysCreate="true" />
</Target>
</Project>

0 comments on commit 842f4d7

Please sign in to comment.