2021-07-15 17:36:53 +03:00
|
|
|
module builtin
|
|
|
|
|
|
|
|
struct map {
|
2021-10-01 21:23:49 +03:00
|
|
|
m JS.Map
|
|
|
|
pub:
|
2021-07-15 17:36:53 +03:00
|
|
|
len int
|
|
|
|
}
|
|
|
|
|
2021-12-15 16:47:34 +03:00
|
|
|
fn (mut m map) internal_set(key JS.Any, val JS.Any) {
|
|
|
|
//$if es5 {
|
|
|
|
#if ('$toJS' in key) key = key.$toJS();
|
|
|
|
#if (!(key in m.val.map)) m.val.length++;
|
|
|
|
#m.val.map[key] = val
|
|
|
|
/*} $else {
|
|
|
|
# if ('$toJS' in key) key = key.$toJS();
|
|
|
|
# m.val.m.set(key,val);
|
|
|
|
}*/
|
|
|
|
_ := key
|
|
|
|
_ := val
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (mut m map) internal_get(key JS.Any) JS.Any {
|
2022-07-21 21:01:30 +03:00
|
|
|
mut val := JS.Any(unsafe { nil })
|
2021-12-15 16:47:34 +03:00
|
|
|
//$if es5 {
|
|
|
|
#if (typeof key != "string" && '$toJS' in key) key = key.$toJS();
|
|
|
|
#val = m.val.map[key]
|
|
|
|
/*} $else {
|
|
|
|
# if ('$toJS' in key) key = key.$toJS();
|
|
|
|
# val = m.val.m.get(key)
|
|
|
|
}*/
|
|
|
|
_ := key
|
|
|
|
return val
|
|
|
|
}
|
|
|
|
|
|
|
|
#map.prototype.get = function (key) { return map_internal_get(this,key); }
|
|
|
|
#map.prototype.set = function(key,val) { map_internal_set(this,key,val); }
|
|
|
|
#map.prototype.has = function (key) { if (typeof key != "string" && '$toJS' in key) { key = key.$toJS() } return key in this.map; }
|
2021-07-15 17:36:53 +03:00
|
|
|
// Removes the mapping of a particular key from the map.
|
|
|
|
[unsafe]
|
2021-12-15 16:47:34 +03:00
|
|
|
pub fn (mut m map) delete(key JS.Any) {
|
|
|
|
#let k = '$toJS' in key ? key.$toJS() : key;
|
|
|
|
|
|
|
|
#if (delete m.val.map[k]) { m.val.length--; };
|
|
|
|
|
|
|
|
_ := key
|
2021-07-15 17:36:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (m &map) free() {}
|
2021-07-24 15:35:17 +03:00
|
|
|
|
2021-10-01 21:23:49 +03:00
|
|
|
//#Object.defineProperty(map.prototype,"len",{get: function() { return this.map.size; }})
|
2021-08-07 17:58:49 +03:00
|
|
|
#map.prototype.toString = function () {
|
|
|
|
#function fmtKey(key) { return typeof key == 'string' ? '\'' + key + '\'' : key}
|
|
|
|
#let res = '{'
|
|
|
|
#for (const entry of this) {
|
|
|
|
#res += fmtKey(entry[0]) + ': ' + entry[0];
|
|
|
|
#}
|
|
|
|
#res += '}'
|
|
|
|
#return res;
|
|
|
|
#}
|
2021-10-03 10:08:21 +03:00
|
|
|
|
|
|
|
#map.prototype.getOrSet = function (key, init) { if (this.map.has(key)) { return this.map.get(key); } else { this.map.set(key,init); return init; } }
|