method.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /**
  2. * @constructor
  3. *
  4. * co_varnames and co_name come from generated code, must access as dict.
  5. */
  6. Sk.builtin.method = function (func, self, klass) {
  7. if (!(this instanceof Sk.builtin.method)) {
  8. Sk.builtin.pyCheckArgs("method", arguments, 3, 3);
  9. if (!Sk.builtin.checkCallable(func)) {
  10. throw new Sk.builtin.TypeError("First argument must be callable");
  11. }
  12. if (self.ob$type === undefined) {
  13. throw new Sk.builtin.TypeError("Second argument must be object of known type");
  14. }
  15. return new Sk.builtin.method(func, self, klass);
  16. }
  17. this.im_func = func;
  18. this.im_self = self;
  19. this.im_class = klass;
  20. this["$d"] = {
  21. im_func: func,
  22. im_self: self,
  23. im_class: klass
  24. };
  25. };
  26. goog.exportSymbol("Sk.builtin.method", Sk.builtin.method);
  27. Sk.abstr.setUpInheritance("instancemethod", Sk.builtin.method, Sk.builtin.object);
  28. Sk.builtin.method.prototype.tp$call = function (args, kw) {
  29. goog.asserts.assert(this.im_self, "should just be a function, not a method since there's no self?");
  30. goog.asserts.assert(this.im_func instanceof Sk.builtin.func);
  31. // 'args' and 'kw' get mucked around with heavily in applyOrSuspend();
  32. // changing it here is OK.
  33. args.unshift(this.im_self);
  34. // A method call is just a call to this.im_func with 'self' on the beginning of the args.
  35. // Do the necessary.
  36. return this.im_func.tp$call(args, kw);
  37. };
  38. Sk.builtin.method.prototype["$r"] = function () {
  39. var name = (this.im_func.func_code && this.im_func.func_code["co_name"] && this.im_func.func_code["co_name"].v) || "<native JS>";
  40. return new Sk.builtin.str("<bound method " + this.im_self.ob$type.tp$name + "." + name +
  41. " of " + this.im_self["$r"]().v + ">");
  42. };