Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

feat: Map support constructor initialization with MapInitialEntries #2941

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

Open
pebrianz wants to merge 7 commits into AssemblyScript:main
base: main
Choose a base branch
Loading
from pebrianz:enhancement/map-initializer
Open
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions NOTICE
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ under the licensing terms detailed in LICENSE:
* Fabián Heredia Montiel <fabianhjr@users.noreply.github.com>
* Jonas Minnberg <sasq64@gmail.com>
* Kam Chehresa <kaz.che@gmail.com>
* Pebrian <pebrianz117@gmail.com>

Portions of this software are derived from third-party works licensed under
the following terms:
Expand Down
10 changes: 9 additions & 1 deletion std/assembly/index.d.ts
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -2394,7 +2394,7 @@ interface NewableFunction extends Function {
interface IArguments {}
interface RegExp {}

declare class Map<K,V> {
interface Map<K,V> {
readonly size: i32;
has(key: K): bool;
set(key: K, value: V): this;
Expand All @@ -2406,6 +2406,14 @@ declare class Map<K,V> {
toString(): string;
}

type MapInitialEntries<K, V> = {key: K, value: V}[]

interface MapConstructor {
new <K, V>(entries?: MapInitialEntries<K, V> | null): Map<K, V>;
}

declare const Map: MapConstructor;

declare class Set<K> {
readonly size: i32;
has(value: K): bool;
Expand Down
46 changes: 44 additions & 2 deletions std/assembly/map.ts
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ function ENTRY_SIZE<K,V>(): usize {
return size;
}

class KeyValue<K,V> {
key: K;
value: V;
}

export type MapInitialEntries<K,V> = KeyValue<K,V>[];

export class Map<K,V> {

// buckets referencing their respective first entry, usize[bucketsMask + 1]
Expand All @@ -66,8 +73,43 @@ export class Map<K,V> {
private entriesOffset: i32 = 0;
private entriesCount: i32 = 0;

constructor() {
/* nop */
constructor(initialEntries: MapInitialEntries<K,V> | null = null) {
if (!initialEntries || !initialEntries.length) return this;
let initialEntriesLength = initialEntries.length;

if (initialEntriesLength >= this.entriesCapacity) this.bucketsMask = initialEntriesLength;
this.rehash((this.bucketsMask << 1) | 1);

for (let i = 0; i < initialEntriesLength; i++) {
let key = initialEntries[i].key;
let value = initialEntries[i].value;
let hashCode = HASH<K>(key);
let entry = this.find(key, hashCode); // unmanaged!
if (entry) {
entry.value = value;
if (isManaged<V>()) {
__link(changetype<usize>(this), changetype<usize>(value), true);
}
} else {
// append new entry
let entries = this.entries;
let entry = changetype<MapEntry<K,V>>(changetype<usize>(entries) + <usize>(this.entriesOffset++) * ENTRY_SIZE<K,V>());
// link with the map
entry.key = key;
if (isManaged<K>()) {
__link(changetype<usize>(this), changetype<usize>(key), true);
}
entry.value = value;
if (isManaged<V>()) {
__link(changetype<usize>(this), changetype<usize>(value), true);
}
++this.entriesCount;
// link with previous entry in bucket
let bucketPtrBase = changetype<usize>(this.buckets) + <usize>(hashCode & this.bucketsMask) * BUCKET_SIZE;
entry.taggedNext = load<usize>(bucketPtrBase);
store<usize>(bucketPtrBase, changetype<usize>(entry));
}
}
}

get size(): i32 {
Expand Down
72 changes: 13 additions & 59 deletions tests/compiler/rt/flags.debug.wat
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
(global $rt/flags/VALUE_ALIGN_REF i32 (i32.const 256))
(global $rt/flags/KEY_ALIGN_REF i32 (i32.const 131072))
(global $~lib/rt/__rtti_base i32 (i32.const 176))
(global $~lib/memory/__data_end i32 (i32.const 392))
(global $~lib/memory/__stack_pointer (mut i32) (i32.const 33160))
(global $~lib/memory/__heap_base i32 (i32.const 33160))
(global $~lib/memory/__data_end i32 (i32.const 456))
(global $~lib/memory/__stack_pointer (mut i32) (i32.const 33224))
(global $~lib/memory/__heap_base i32 (i32.const 33224))
(memory 0ドル 1)
(data 0ドル (i32.const 12) "<00円00円00円00円00円00円00円00円00円00円00円02円00円00円00円$00円00円00円I00円n00円d00円e00円x00円 00円o00円u00円t00円 00円o00円f00円 00円r00円a00円n00円g00円e00円00円00円00円00円00円00円00円00円")
(data 1ドル (i32.const 76) ",00円00円00円00円00円00円00円00円00円00円00円02円00円00円00円14円00円00円00円~00円l00円i00円b00円/00円r00円t00円.00円t00円s00円00円00円00円00円00円00円00円00円")
(data 2ドル (i32.const 124) ",00円00円00円00円00円00円00円00円00円00円00円02円00円00円00円16円00円00円00円r00円t00円/00円f00円l00円a00円g00円s00円.00円t00円s00円00円00円00円00円00円00円")
(data 3ドル (i32.const 176) "500円00円00円 00円00円00円 00円00円00円 00円00円00円00円00円00円00円A08円00円00円A00円00円00円A00円00円00円81円08円00円00円81円00円00円00円01円\t00円00円01円01円00円00円01円\n00円00円01円02円00円00円01円19円00円00円01円1円a00円00円B08円00円00円B00円00円00円82円08円00円00円82円00円00円00円02円\t00円00円02円01円00円00円02円\n00円00円02円02円00円00円02円19円00円00円02円1円a00円00円02円04円00円00円 00円00円00円02円A00円00円02円a00円00円H08円00円00円H00円00円00円88円08円00円00円88円00円00円00円08円\t00円00円08円01円00円00円08円\n00円00円08円02円00円00円08円19円00円00円08円1円a00円00円08円04円00円00円08円A00円00円08円a00円\00P08円08円\0090円08円14円\0010円\t12円00円\10\n11円00円10円84円10円\00P08円82円00円P08円\c200円10円\c110円00円10円\e110円00円10円a\c200円10円\t200円")
(data 3ドル (i32.const 176) "E00円00円00円 00円00円00円 00円00円00円 00円00円00円00円00円00円00円A08円00円00円A00円00円00円A00円00円00円81円08円00円00円81円00円00円00円01円\t00円00円01円01円00円00円01円\n00円00円01円02円00円00円01円19円00円00円01円1円a00円00円B08円00円00円B00円00円00円82円08円00円00円82円00円00円00円02円\t00円00円02円01円00円00円02円\n00円00円02円02円00円00円02円19円00円00円02円1円a00円00円02円04円00円00円 00円00円00円02円A00円00円02円a00円00円H08円00円00円H00円00円00円88円08円00円00円88円00円00円00円08円\t00円00円08円01円00円00円08円\n00円00円08円02円00円00円08円19円00円00円08円1円a00円00円08円04円00円00円08円A00円00円08円a00円\0090円08円\1400円 00円00円00円02円A\00\00\10\t12円00円 00円00円00円02円A00円00円\10\n11円00円 00円00円00円02円A00円\00P08円82円\0000円00円00円00円02円A00円\00P08円\c200円\0000円00円00円02円A00円00円\10\c110円00円\0000円00円00円02円A00円00円\10\e110円00円\0000円00円00円02円A00円00円\10a\c200円\0000円00円00円02円A00円00円\10\t200円 00円00円00円02円A00円00円")
(table 0ドル 1 1 funcref)
(elem 0ドル (i32.const 1))
(export "memory" (memory 0ドル))
Expand Down Expand Up @@ -607,23 +607,8 @@
unreachable
end
)
(func $"rt/flags/test<~lib/map/Map<v128,i8>>" (param $flags i32)
i32.const 42
call $~lib/rt/__typeinfo
local.get $flags
i32.eq
i32.eqz
if
i32.const 0
i32.const 144
i32.const 6
i32.const 3
call $~lib/builtins/abort
unreachable
end
)
(func $"rt/flags/test<~lib/map/Map<i64,i16>>" (param $flags i32)
i32.const 43
i32.const 42
call $~lib/rt/__typeinfo
local.get $flags
i32.eq
Expand All @@ -638,21 +623,6 @@
end
)
(func $"rt/flags/test<~lib/map/Map<i32,i32>>" (param $flags i32)
i32.const 44
call $~lib/rt/__typeinfo
local.get $flags
i32.eq
i32.eqz
if
i32.const 0
i32.const 144
i32.const 6
i32.const 3
call $~lib/builtins/abort
unreachable
end
)
(func $"rt/flags/test<~lib/map/Map<i16,i64>>" (param $flags i32)
i32.const 45
call $~lib/rt/__typeinfo
local.get $flags
Expand All @@ -667,8 +637,8 @@
unreachable
end
)
(func $"rt/flags/test<~lib/map/Map<i8,v128>>" (param $flags i32)
i32.const 46
(func $"rt/flags/test<~lib/map/Map<i16,i64>>" (param $flags i32)
i32.const 48
call $~lib/rt/__typeinfo
local.get $flags
i32.eq
Expand All @@ -683,7 +653,7 @@
end
)
(func $"rt/flags/test<~lib/map/Map<rt/flags/Ref,i8>>" (param $flags i32)
i32.const 47
i32.const 51
call $~lib/rt/__typeinfo
local.get $flags
i32.eq
Expand All @@ -698,7 +668,7 @@
end
)
(func $"rt/flags/test<~lib/map/Map<rt/flags/Ref|null,i8>>" (param $flags i32)
i32.const 48
i32.const 54
call $~lib/rt/__typeinfo
local.get $flags
i32.eq
Expand All @@ -713,7 +683,7 @@
end
)
(func $"rt/flags/test<~lib/map/Map<i8,rt/flags/Ref>>" (param $flags i32)
i32.const 49
i32.const 57
call $~lib/rt/__typeinfo
local.get $flags
i32.eq
Expand All @@ -728,7 +698,7 @@
end
)
(func $"rt/flags/test<~lib/map/Map<i8,rt/flags/Ref|null>>" (param $flags i32)
i32.const 50
i32.const 60
call $~lib/rt/__typeinfo
local.get $flags
i32.eq
Expand All @@ -743,7 +713,7 @@
end
)
(func $"rt/flags/test<~lib/map/Map<rt/flags/Ref|null,rt/flags/Ref|null>>" (param $flags i32)
i32.const 51
i32.const 63
call $~lib/rt/__typeinfo
local.get $flags
i32.eq
Expand All @@ -758,7 +728,7 @@
end
)
(func $"rt/flags/test<~lib/map/Map<f32,i32>>" (param $flags i32)
i32.const 52
i32.const 66
call $~lib/rt/__typeinfo
local.get $flags
i32.eq
Expand Down Expand Up @@ -982,14 +952,6 @@
i32.or
call $rt/flags/test<~lib/set/Set<rt/flags/Ref|null>>
i32.const 16
i32.const 524288
i32.or
i32.const 64
i32.or
i32.const 2048
i32.or
call $"rt/flags/test<~lib/map/Map<v128,i8>>"
i32.const 16
i32.const 262144
i32.or
i32.const 1048576
Expand Down Expand Up @@ -1020,14 +982,6 @@
i32.or
call $"rt/flags/test<~lib/map/Map<i16,i64>>"
i32.const 16
i32.const 32768
i32.or
i32.const 1048576
i32.or
i32.const 1024
i32.or
call $"rt/flags/test<~lib/map/Map<i8,v128>>"
i32.const 16
global.get $rt/flags/KEY_ALIGN_REF
i32.or
i32.const 8388608
Expand Down
28 changes: 9 additions & 19 deletions tests/compiler/rt/flags.release.wat
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
(data 1ドル.1 (i32.const 1112) "02円00円00円00円14円00円00円00円~00円l00円i00円b00円/00円r00円t00円.00円t00円s")
(data 2ドル (i32.const 1148) ",")
(data 2ドル.1 (i32.const 1160) "02円00円00円00円16円00円00円00円r00円t00円/00円f00円l00円a00円g00円s00円.00円t00円s")
(data 3ドル (i32.const 1200) "500円00円00円 00円00円00円 00円00円00円 00円00円00円00円00円00円00円A08円00円00円A00円00円00円A00円00円00円81円08円00円00円81円00円00円00円01円\t00円00円01円01円00円00円01円\n00円00円01円02円00円00円01円19円00円00円01円1円a00円00円B08円00円00円B00円00円00円82円08円00円00円82円00円00円00円02円\t00円00円02円01円00円00円02円\n00円00円02円02円00円00円02円19円00円00円02円1円a00円00円02円04円00円00円 00円00円00円02円A00円00円02円a00円00円H08円00円00円H00円00円00円88円08円00円00円88円00円00円00円08円\t00円00円08円01円00円00円08円\n00円00円08円02円00円00円08円19円00円00円08円1円a00円00円08円04円00円00円08円A00円00円08円a00円\00P08円08円\0090円08円14円\0010円\t12円00円\10\n11円00円10円84円10円\00P08円82円00円P08円\c200円10円\c110円00円10円\e110円00円10円a\c200円10円\t2")
(data 3ドル (i32.const 1200) "E00円00円00円 00円00円00円 00円00円00円 00円00円00円00円00円00円00円A08円00円00円A00円00円00円A00円00円00円81円08円00円00円81円00円00円00円01円\t00円00円01円01円00円00円01円\n00円00円01円02円00円00円01円19円00円00円01円1円a00円00円B08円00円00円B00円00円00円82円08円00円00円82円00円00円00円02円\t00円00円02円01円00円00円02円\n00円00円02円02円00円00円02円19円00円00円02円1円a00円00円02円04円00円00円 00円00円00円02円A00円00円02円a00円00円H08円00円00円H00円00円00円88円08円00円00円88円00円00円00円08円\t00円00円08円01円00円00円08円\n00円00円08円02円00円00円08円19円00円00円08円1円a00円00円08円04円00円00円08円A00円00円08円a00円\0090円08円\1400円 00円00円00円02円A\00\00\10\t12円00円 00円00円00円02円A00円00円\10\n11円00円 00円00円00円02円A00円\00P08円82円\0000円00円00円00円02円A00円\00P08円\c200円\0000円00円00円02円A00円00円\10\c110円00円\0000円00円00円02円A00円00円\10\e110円00円\0000円00円00円02円A00円00円\10a\c200円\0000円00円00円02円A00円00円\10\t200円 00円00円00円02円A")
(export "memory" (memory 0ドル))
(start $~start)
(func $~lib/rt/__typeinfo (param 0ドル i32) (result i32)
Expand Down Expand Up @@ -222,55 +222,45 @@
br_if $folding-inner0
i32.const 42
call $~lib/rt/__typeinfo
i32.const 526416
i32.ne
br_if $folding-inner0
i32.const 43
call $~lib/rt/__typeinfo
i32.const 1312912
i32.ne
br_if $folding-inner0
i32.const 44
i32.const 45
call $~lib/rt/__typeinfo
i32.const 1181968
i32.ne
br_if $folding-inner0
i32.const 45
i32.const 48
call $~lib/rt/__typeinfo
i32.const 1116688
i32.ne
br_if $folding-inner0
i32.const 46
call $~lib/rt/__typeinfo
i32.const 1082384
i32.ne
br_if $folding-inner0
i32.const 47
i32.const 51
call $~lib/rt/__typeinfo
i32.const 8521808
i32.ne
br_if $folding-inner0
i32.const 48
i32.const 54
call $~lib/rt/__typeinfo
i32.const 12716112
i32.ne
br_if $folding-inner0
i32.const 49
i32.const 57
call $~lib/rt/__typeinfo
i32.const 1098000
i32.ne
br_if $folding-inner0
i32.const 50
i32.const 60
call $~lib/rt/__typeinfo
i32.const 1106192
i32.ne
br_if $folding-inner0
i32.const 51
i32.const 63
call $~lib/rt/__typeinfo
i32.const 12738832
i32.ne
br_if $folding-inner0
i32.const 52
i32.const 66
call $~lib/rt/__typeinfo
i32.const 3279120
i32.ne
Expand Down
4 changes: 2 additions & 2 deletions tests/compiler/rt/flags.ts
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ test<Set<v128>>(TypeinfoFlags.SET | TypeinfoFlags.VALUE_ALIGN_4);
test<Set<Ref>>(TypeinfoFlags.SET | VALUE_ALIGN_REF | TypeinfoFlags.VALUE_MANAGED);
test<Set<Ref | null>>(TypeinfoFlags.SET | VALUE_ALIGN_REF | TypeinfoFlags.VALUE_NULLABLE | TypeinfoFlags.VALUE_MANAGED);

test<Map<v128,i8>>(TypeinfoFlags.MAP | TypeinfoFlags.KEY_ALIGN_4 | TypeinfoFlags.VALUE_ALIGN_0 | TypeinfoFlags.VALUE_SIGNED);
// test<Map<v128,i8>>(TypeinfoFlags.MAP | TypeinfoFlags.KEY_ALIGN_4 | TypeinfoFlags.VALUE_ALIGN_0 | TypeinfoFlags.VALUE_SIGNED);
test<Map<i64,i16>>(TypeinfoFlags.MAP | TypeinfoFlags.KEY_ALIGN_3 | TypeinfoFlags.KEY_SIGNED | TypeinfoFlags.VALUE_ALIGN_1 | TypeinfoFlags.VALUE_SIGNED);
test<Map<i32,i32>>(TypeinfoFlags.MAP | TypeinfoFlags.KEY_ALIGN_2 | TypeinfoFlags.KEY_SIGNED | TypeinfoFlags.VALUE_ALIGN_2 | TypeinfoFlags.VALUE_SIGNED);
test<Map<i16,i64>>(TypeinfoFlags.MAP | TypeinfoFlags.KEY_ALIGN_1 | TypeinfoFlags.KEY_SIGNED | TypeinfoFlags.VALUE_ALIGN_3 | TypeinfoFlags.VALUE_SIGNED);
test<Map<i8,v128>>(TypeinfoFlags.MAP | TypeinfoFlags.KEY_ALIGN_0 | TypeinfoFlags.KEY_SIGNED | TypeinfoFlags.VALUE_ALIGN_4);
// test<Map<i8,v128>>(TypeinfoFlags.MAP | TypeinfoFlags.KEY_ALIGN_0 | TypeinfoFlags.KEY_SIGNED | TypeinfoFlags.VALUE_ALIGN_4);
test<Map<Ref,i8>>(TypeinfoFlags.MAP | KEY_ALIGN_REF | TypeinfoFlags.KEY_MANAGED | TypeinfoFlags.VALUE_ALIGN_0 | TypeinfoFlags.VALUE_SIGNED);
test<Map<Ref | null,i8>>(TypeinfoFlags.MAP | KEY_ALIGN_REF | TypeinfoFlags.KEY_NULLABLE | TypeinfoFlags.KEY_MANAGED | TypeinfoFlags.VALUE_ALIGN_0 | TypeinfoFlags.VALUE_SIGNED);
test<Map<i8,Ref>>(TypeinfoFlags.MAP | TypeinfoFlags.KEY_ALIGN_0 | TypeinfoFlags.KEY_SIGNED | TypeinfoFlags.VALUE_MANAGED | VALUE_ALIGN_REF);
Expand Down
Loading

AltStyle によって変換されたページ (->オリジナル) /