mirror of
https://git.savannah.gnu.org/git/guile.git
synced 2025-04-30 20:00:19 +02:00
Implement hashtable built-ins
* module/language/js-il/runtime.js (scheme.HashTable): New Constructor. (make-hash-table, hash-clear!, hashq-remove!, hashq-ref, hashq-set!, hash-for-each): Implement built-ins.
This commit is contained in:
parent
30cc1e0751
commit
bfaf07091a
1 changed files with 79 additions and 0 deletions
|
@ -936,3 +936,82 @@ def_guile0("make-struct", function (self, cont, vtable, tailsize) {
|
|||
}
|
||||
});
|
||||
|
||||
// Hashtables
|
||||
def_guile0("make-hash-table", function (self, cont, size) {
|
||||
return cont(new scheme.HashTable());
|
||||
});
|
||||
|
||||
def_guile0("make-weak-key-hash-table", function (self, cont, size) {
|
||||
// FIXME: not weak
|
||||
return cont(new scheme.HashTable());
|
||||
});
|
||||
|
||||
def_guile0("hash-clear!", function (self, cont, hashtable) {
|
||||
if (hashtable instanceof scheme.HashTable) {
|
||||
hashtable.table = {};
|
||||
return cont(scheme.FALSE);
|
||||
} else {
|
||||
console.log("hash-clear!", arguments);
|
||||
not_implemented_yet();
|
||||
}
|
||||
});
|
||||
|
||||
def_guile0("hashq-remove!", function (self, cont, htable, key) {
|
||||
if (htable instanceof scheme.HashTable) {
|
||||
delete htable.table[scm_hash(key)];
|
||||
return cont(scheme.FALSE);
|
||||
} else {
|
||||
console.log("hashq-ref", arguments);
|
||||
not_implemented_yet();
|
||||
}
|
||||
});
|
||||
|
||||
var scm_hash = function (obj) {
|
||||
if (obj instanceof scheme.Symbol) {
|
||||
return obj.name;
|
||||
}
|
||||
|
||||
console.log("Can't hash object", obj);
|
||||
throw "BadHash";
|
||||
};
|
||||
|
||||
scheme.HashTable = function ( ) {
|
||||
this.table = {};
|
||||
this.lookup = function (obj, dflt) {
|
||||
var hash = scm_hash(obj);
|
||||
if (this.table.hasOwnProperty(hash)) {
|
||||
return this.table[hash];
|
||||
} else {
|
||||
return dflt;
|
||||
}
|
||||
};
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
def_guile0("hashq-ref", function(self, cont, obarray, sym, dflt) {
|
||||
|
||||
if (obarray instanceof scheme.HashTable) {
|
||||
return cont(obarray.lookup(sym, dflt ? dflt : scheme.FALSE));
|
||||
} else {
|
||||
console.log("hashq-ref", arguments);
|
||||
not_implemented_yet();
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
def_guile0("hashq-set!", function (self, cont, hashtable, key, obj) {
|
||||
if (hashtable instanceof scheme.HashTable) {
|
||||
hashtable.table[scm_hash(key)] = obj;
|
||||
return cont(scheme.FALSE);
|
||||
} else {
|
||||
console.log("hashq-set!", arguments);
|
||||
not_implemented_yet();
|
||||
}
|
||||
});
|
||||
|
||||
def_guile0("hash-for-each", function (self, cont, module, symbol) {
|
||||
// FIXME:
|
||||
return cont(scheme.FALSE);
|
||||
});
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue