Skip to content

Commit

Permalink
+ Added Linear Congruential Generator PRNG function to generate a
Browse files Browse the repository at this point in the history
   unique ID per object. This allows us to implement an id() function
   that returns a unique id, and create more realistic-looking repr()
   strings.
 + Implemented id()
  • Loading branch information
Christian Iversen committed Jan 15, 2012
1 parent 41738f5 commit c36f054
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
7 changes: 7 additions & 0 deletions pyjaco/stdlib/05-init.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,17 @@

var $PY = {};

$PY.prng = 42;

var __builtins__ = {};

__builtins__.PY$__python3__ = false;

function prng() {
$PY.prng = ($PY.prng * 0x8088405 + 1) % 0xFFFFFFFF;
return $PY.prng;
}

function bt() {
try {
null();
Expand Down
5 changes: 4 additions & 1 deletion pyjaco/stdlib/10-builtin.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,10 @@ __builtins__.PY$hex = function(num) {
}
};

__builtins__.PY$id = $PY.c_nif;
__builtins__.PY$id = function(obj) {
return __builtins__.PY$int(obj.id);
}

__builtins__.PY$input = $PY.c_nif;

__builtins__.PY$intern = function(x) {
Expand Down
3 changes: 2 additions & 1 deletion pyjaco/stdlib/11-classes.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ object.PY$__create__ = function(cls) {
};

obj.PY$__class__ = cls;
obj.id = prng();
obj.PY$__init__.apply(obj, args);
return obj;
};
Expand Down Expand Up @@ -121,7 +122,7 @@ object.PY$__delattr__ = function(k) {

object.PY$__repr__ = function() {
if (this.PY$__class__) {
return str("<instance of " + this.PY$__class__.PY$__name__ + " at 0xPYJACO>");
return str("<instance of " + this.PY$__class__.PY$__name__ + " at 0x" + this.id.toString(16) + ">");
} else if (this.PY$__name__) {
return str("<type '" + this.PY$__name__ + "'>");
} else {
Expand Down

0 comments on commit c36f054

Please sign in to comment.