You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

OrdinarySetPrototypeOf.js 1.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. 'use strict';
  2. var GetIntrinsic = require('../GetIntrinsic');
  3. var $TypeError = GetIntrinsic('%TypeError%');
  4. var $setProto = require('../helpers/setProto');
  5. var OrdinaryGetPrototypeOf = require('./OrdinaryGetPrototypeOf');
  6. var Type = require('./Type');
  7. // https://ecma-international.org/ecma-262/7.0/#sec-ordinarysetprototypeof
  8. module.exports = function OrdinarySetPrototypeOf(O, V) {
  9. if (Type(V) !== 'Object' && Type(V) !== 'Null') {
  10. throw new $TypeError('Assertion failed: V must be Object or Null');
  11. }
  12. /*
  13. var extensible = IsExtensible(O);
  14. var current = OrdinaryGetPrototypeOf(O);
  15. if (SameValue(V, current)) {
  16. return true;
  17. }
  18. if (!extensible) {
  19. return false;
  20. }
  21. */
  22. try {
  23. $setProto(O, V);
  24. } catch (e) {
  25. return false;
  26. }
  27. return OrdinaryGetPrototypeOf(O) === V;
  28. /*
  29. var p = V;
  30. var done = false;
  31. while (!done) {
  32. if (p === null) {
  33. done = true;
  34. } else if (SameValue(p, O)) {
  35. return false;
  36. } else {
  37. if (wat) {
  38. done = true;
  39. } else {
  40. p = p.[[Prototype]];
  41. }
  42. }
  43. }
  44. O.[[Prototype]] = V;
  45. return true;
  46. */
  47. };