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

initial tests for arguments length #1898

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/builtins.ts
Expand Up @@ -705,6 +705,8 @@ export namespace BuiltinNames {
export const wasiAbort = "~lib/wasi/index/abort";
export const wasiTrace = "~lib/wasi/index/trace";
export const wasiSeed = "~lib/wasi/index/seed";

jtenner marked this conversation as resolved.
Show resolved Hide resolved
export const arguments_length = "~lib/builtins/arguments.get:length";
}

/** Builtin compilation context. */
Expand Down Expand Up @@ -9421,6 +9423,17 @@ function builtin_f64x2_ge(ctx: BuiltinContext): ExpressionRef {
}
builtins.set(BuiltinNames.f64x2_ge, builtin_f64x2_ge);

// access arguments.length
function builtin_arguments_length(ctx: BuiltinContext): ExpressionRef {
const compiler = ctx.compiler;
const mod = compiler.module;
compiler.currentType = Type.i32;
return mod.i32(1);
}

builtins.set(BuiltinNames.arguments_length, builtin_arguments_length);


// f64x2.convert_low_i32x4_s -> v128.convert_low<i32>
function builtin_f64x2_convert_low_i32x4_s(ctx: BuiltinContext): ExpressionRef {
checkTypeAbsent(ctx);
Expand Down
9 changes: 8 additions & 1 deletion std/assembly/builtins.ts
Expand Up @@ -2336,4 +2336,11 @@ declare function trace(
@external("env", "seed")
declare function seed(): f64;

/* eslint-enable @typescript-eslint/no-unused-vars */
@final
export declare class arguments {
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it possible to declare a namespace? If not, that should be an added feature request.

// @ts-ignore: builtin
@builtin
static get length(): i32;
}

/* eslint-enable @typescript-eslint/no-unused-vars */
10 changes: 9 additions & 1 deletion std/assembly/index.d.ts
Expand Up @@ -1906,7 +1906,9 @@ interface Function {
/** Returns a string representation of this function. */
toString(): string;
}
interface IArguments {}
interface IArguments {
length: i32;
}
interface RegExp {}

declare class Map<K,V> {
Expand Down Expand Up @@ -2211,3 +2213,9 @@ declare function external(...args: any[]): any;

/** Annotates a global for lazy compilation. */
declare function lazy(...args: any[]): any;

/** Global arguments namespace for information about function calls. */
declare namespace arguments {
/** The number of arguments passed to the function call. */
export const length: i32;
}
7 changes: 7 additions & 0 deletions tests/compiler/builtins.ts
Expand Up @@ -583,3 +583,10 @@ function rotr3(a: i8, b: i8, c: i8): i32 {
return rotr(a, rotr(b, c));
}
assert(rotr3(48, 8, 1) == 3);


function arglength1(a: i32): void {
assert(arguments.length == 1);
}

arglength1(1);