Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Class definition order in generated JavaScript wrt. inheritance hierarchy #600

Closed
DartBot opened this issue Nov 25, 2011 · 5 comments
Closed
Assignees

Comments

@DartBot
Copy link

DartBot commented Nov 25, 2011

This issue was originally filed by f.jo...@gmail.com


The $inherits function uses one of two methods: 1) rerouting the proto property of the child's prototype, and 2) replacing the child's prototype with a new object whose prototype is the parent's prototype.

Method 1 is used when the proto property is available, method 2 otherwise.

Method 2 has known ordering issues (see issue #168, fixed), but there is at least one other issue that is not fixed yet:

Let C extend B, and B extend A.

  • If these classes are defined in the order: A, B, and C, then inheritance works correctly (now that issue Isolate method ordering in generated JS #168 is fixed).
  • However if they are defined in the order: C, A, B, then inheritance does not work correctly: when B is made to inherit from A, its prototype is replaced, and the old prototype (which was used to make C inherit from B) is not "connected" anymore. Therefore C is no longer correctly inheriting from B.

Note that this issue does not occur with just two levels of class inheritance. This is the reason why the example above uses three classes.

One solution to this issue involves properly ordering class definitions in generated JavaScript code so that superclasses come before their child classes (e.g., making sure that A, B, and C are defined in this order).

This issue depends on three things: a) how the $inherits method implements inheritance when the proto property is not available, b) how the compiler orders class definitions in generated JavaScript code, and c) that the generated code is executed on a JavaScript engine which does not offer the proto property.

Therefore, this issue does not occur as such if no JavaScript code is generated. Moreover, it can only be seen when there are at least three levels of class inheritance in the code to compile, and when the compiler randomly generates an improper order.

This issue has been identified in the frog compiler r1848, but may exist in any other dart to JavaScript compiler that uses a similar $inherits function, and does not order class definitions properly.

Two additional remarks:

  • This issue currently prevents the frog compiler (r1848) itself to correctly run when the proto property is not available: PartialFunctionElement extends FunctionElement, which extends Element, but they are defined in the following order: PartialFunctionElement, Element, and FunctionElement (note that there may be other occurrences of this issue as well). The error messages are not easy to link to this issue.
  • I would advise that the tests on the frog compiler be run on at least two different JavaScript engines: one supporting the proto property, as well as another that does not.
@DartBot
Copy link
Author

DartBot commented Nov 28, 2011

This comment was originally written by drfibonacci@google.com


Added Area-Frog, Triaged labels.

@jmesserly
Copy link

Set owner to @efortuna.
Added Started label.

@DartBot
Copy link
Author

DartBot commented Dec 7, 2011

This comment was originally written by f.jouau...@gmail.com


Here are some additional notes:

  • In addition to reordering the pieces of code generated for each class, it is also necessary to place the initialization of the prototypes of additional constructors (i.e., suffixed $ctor in the generated Javascript) after the call to $inherits. This is #­168, which was about methods, extended to additional constructors.
  • It should also be possible to simply put all calls to $inherits at the beginning of the generated code in the proper order (i.e., it is not strictly necessary to reorder everything). They do not need to come after main constructor definition since functions are added to the variable object before code execution.
  • It is currently possible to make minfrog (or even archived versions of frogsh) work without actually reordering much code. However, this involves replacing $inherits with a function that remembers all inheritance relationships, and applies them in proper order at the end (taking care to copy old children prototype properties to new children prototype). Moreover, a few other things have to be taken care of (like additional constructors, and calls to $inheritsMembers.
  • Other possibilites might also work, like: adding code to $inherits to copy properties of old prototype to new prototype (but this still requires placing additional constructor prototype initialization after calls to $inherits). However, my personal preference still goes to ordering classes properly.

@efortuna
Copy link
Contributor

efortuna commented Dec 7, 2011

Thanks for all the additional info. I have a fix already implemented that should fix all of these issues, I am just working on adding tests before I check in, so that this doesn't get broken again!

@efortuna
Copy link
Contributor

Added Fixed label.

nex3 pushed a commit that referenced this issue Aug 31, 2016
copybara-service bot pushed a commit that referenced this issue May 20, 2022
Changes:
```
> git log --format="%C(auto) %h %s" c1eb6cb..b149f80
 https://dart.googlesource.com/protobuf.git/+/b149f80 Remove the top level analysis_options.yaml file (#656)
 https://dart.googlesource.com/protobuf.git/+/8f6f307 Fix comment syntax
 https://dart.googlesource.com/protobuf.git/+/665f7b0 Remove trailing whitespace in protobuf/LICENSE
 https://dart.googlesource.com/protobuf.git/+/9ffbaf2 Fix default list type for frozen messages (#654)
 https://dart.googlesource.com/protobuf.git/+/a68bb5a Fix Fixed32 to be parsed as unsigned when parsing proto3 protos (#655)
 https://dart.googlesource.com/protobuf.git/+/6957c98 Convert the field started with underscore and digit to a legal name (#651)
 https://dart.googlesource.com/protobuf.git/+/71defca Account for double.infinity and double.nan in json serializers (#652)
 https://dart.googlesource.com/protobuf.git/+/5a48349 Allow `Timestamp.toDateTime()` to return a `DateTime` in local timezone (#653)
 https://dart.googlesource.com/protobuf.git/+/117e869 Make the toString of enums be the value if names are omitted (#649)
 https://dart.googlesource.com/protobuf.git/+/88c4016 Align the hashCode of a message with an empty unknown field-set with that of no unknown field set (#648)
 https://dart.googlesource.com/protobuf.git/+/eed09c4 Fix proto3 repeated field encoding without packed option (#635)
 https://dart.googlesource.com/protobuf.git/+/8f587b1 Simplify `_FieldSet` getters (#646)
 https://dart.googlesource.com/protobuf.git/+/494f189 Fix compile-protos.sh interpreter (#645)
 https://dart.googlesource.com/protobuf.git/+/5e1a422 Fix typo in protobuf-0.9.0+1 changelog
 https://dart.googlesource.com/protobuf.git/+/46df68a Add `UseResult` annotation to `rebuild` (#631)
 https://dart.googlesource.com/protobuf.git/+/ff5304f Migrate protoc_plugin to null safety (#642)
 https://dart.googlesource.com/protobuf.git/+/657197d Fix typo in GeneratedMessage.copyWith documentation (#641)
 https://dart.googlesource.com/protobuf.git/+/e30a522 Fix sharing coded buffer bytes when parsing `bytes` fields (#640)
 https://dart.googlesource.com/protobuf.git/+/810b166 Clear unknown field when setting an extension field with the same tag (#639)
 https://dart.googlesource.com/protobuf.git/+/d30623b Treat empty and uninitialized Maps the same in equality checks (#638)
 https://dart.googlesource.com/protobuf.git/+/4fe3ee4 Make `MapFieldInfo` key and value types non-nullable (#600)
 https://dart.googlesource.com/protobuf.git/+/c26ac34 Add grpc example to protoc_plugin README (#514)
 https://dart.googlesource.com/protobuf.git/+/c35d787 Revert changes to reserved names to maintain backwards compat (#636)
 https://dart.googlesource.com/protobuf.git/+/146b186 Remove unused `GeneratedMessage` constructors (#634)
 https://dart.googlesource.com/protobuf.git/+/1b12ac9 Remove a closure in `_FieldSet.hashCode` (#633)
 https://dart.googlesource.com/protobuf.git/+/5731242 Minor fix in query_bench set_nested_value benchmark: (#630)
 https://dart.googlesource.com/protobuf.git/+/767ce81 Remove fold() closures from `FieldSet._hashCode`. (#554)
 https://dart.googlesource.com/protobuf.git/+/99bc541 protoc_plugin README fixes and tweaks: (#617)
 https://dart.googlesource.com/protobuf.git/+/e282e17 protobuf benchs: invoke protoc once with all protos (#623)
 https://dart.googlesource.com/protobuf.git/+/bef672b protobuf benchs: fix old --trust-type-annotations flag (#622)
 https://dart.googlesource.com/protobuf.git/+/d072e5f dependabot: check for updates monthly (#620)
 https://dart.googlesource.com/protobuf.git/+/96bdf38 Fix TypeRegistry passing when unpacking nested Any messages from JSON (#568)
 https://dart.googlesource.com/protobuf.git/+/4ec722a Correctly combine hash codes for repeated enums. (#556)
 https://dart.googlesource.com/protobuf.git/+/b96dc21 Testing: don't return static methods in findMemberNames (#618)
 https://dart.googlesource.com/protobuf.git/+/09e8a8d Update protobuf/benchmarks README for #553
 https://dart.googlesource.com/protobuf.git/+/3ef4539 Add a benchmark for computing hashCodes. (#553)
 https://dart.googlesource.com/protobuf.git/+/d232e6e Tweak READMEs: (#610)
 https://dart.googlesource.com/protobuf.git/+/0f01fa9 Update protobuf pubspec.yaml (#616)
 https://dart.googlesource.com/protobuf.git/+/a10426b Update protoc_plugin pubspec.yaml (#615)
 https://dart.googlesource.com/protobuf.git/+/a0021c7 Make `protoName` unCamelCase lazy (#606)
 https://dart.googlesource.com/protobuf.git/+/ded1ac7 Document map key and value fields (#603)
 https://dart.googlesource.com/protobuf.git/+/8792f2a Remove redundant check in PbMap equality check (#604)
 https://dart.googlesource.com/protobuf.git/+/b7f9569 Tweak BuilderInfo and FieldInfo docs: (#597)
 https://dart.googlesource.com/protobuf.git/+/6f85c32 Remove a redundant cast (#598)
 https://dart.googlesource.com/protobuf.git/+/3df8669 Latest mono_repo (#601)
 https://dart.googlesource.com/protobuf.git/+/9da84ae Fix a potential issue in CodedBufferWriter (#594)
 https://dart.googlesource.com/protobuf.git/+/6be405f Remove old and unused test file (#589)
 https://dart.googlesource.com/protobuf.git/+/900cef5 Fix protoc_plugin run-tests make rule (#586)
 https://dart.googlesource.com/protobuf.git/+/2546269 Fix rounding when handling negative timestamps (#580)
 https://dart.googlesource.com/protobuf.git/+/8afce8d Fix Readme `pub get` instead of `pub install`. (#486)
 https://dart.googlesource.com/protobuf.git/+/782fd24 Avoid runtime function type check in lazily created singleton creator functions (#574)
 https://dart.googlesource.com/protobuf.git/+/a7e75cb Update README.md
 https://dart.googlesource.com/protobuf.git/+/23136dc Version bump (#563)
 https://dart.googlesource.com/protobuf.git/+/18346f5 Convert null keys and values to default when parsing map entries (#536)
 https://dart.googlesource.com/protobuf.git/+/bb4cf0b Bump to latest mono_repo - use latest actions (#561)
 https://dart.googlesource.com/protobuf.git/+/835ab75 Remove unneeded imports
 https://dart.googlesource.com/protobuf.git/+/ef733ac latest mono_repo
 https://dart.googlesource.com/protobuf.git/+/ecfb862 Fix test for latest analysis (#543)
 https://dart.googlesource.com/protobuf.git/+/26a0a26 fix insecure link in protoc_plugin readme
 https://dart.googlesource.com/protobuf.git/+/444e855 Bump dart-lang/setup-dart from 1.1 to 1.2 (#535)
 https://dart.googlesource.com/protobuf.git/+/d3e0e4a Fix analysis options
 https://dart.googlesource.com/protobuf.git/+/cf29280 Fix comment references

```

Diff: https://dart.googlesource.com/protobuf.git/+/c1eb6cb51af39ccbaa1a8e19349546586a5c8e31~..b149f801cf7a5e959cf1dbf72d61068ac275f24b/

Tested: relying on existing SDK and protobuf tests
Change-Id: I7f89b998a0aba13999d180ee9814a26a5f1d054d
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/245228
Reviewed-by: Alexander Markov <alexmarkov@google.com>
Reviewed-by: Ömer Ağacan <omersa@google.com>
Commit-Queue: Devon Carew <devoncarew@google.com>
copybara-service bot pushed a commit that referenced this issue Jan 31, 2023
…g, mockito, package_config, shelf, string_scanner, test, webdev

Revisions updated by `dart tools/rev_sdk_deps.dart`.

async (https://github.com/dart-lang/async/compare/f700e9a..f700e9a):
  f700e9a  2023-01-27  Devon Carew  blast_repo fixes (#231)

characters (https://github.com/dart-lang/characters/compare/4526aa8..4526aa8):
  4526aa8  2023-01-30  Lasse R.H. Nielsen  Update tables to Unicode 15.0. (#71)

collection (https://github.com/dart-lang/collection/compare/a566328..a566328):
  a566328  2023-01-26  Devon Carew  add a publish script; prep to publish (#267)

dartdoc (https://github.com/dart-lang/dartdoc/compare/bc7bdc4..bc7bdc4):
  bc7bdc44  2023-01-30  dependabot[bot]  Bump js from 0.6.5 to 0.6.7 (#3310)

json_rpc_2 (https://github.com/dart-lang/json_rpc_2/compare/e73c4ad..e73c4ad):
  e73c4ad  2023-01-26  Devon Carew  blast_repo fixes (#89)

logging (https://github.com/dart-lang/logging/compare/399100a..399100a):
  399100a  2023-01-26  Devon Carew  add a publish script; prep to publish 1.1.1 (#128)

mockito (https://github.com/dart-lang/mockito/compare/d2a8df1..d2a8df1):
  d2a8df1  2023-01-30  Kevin Moore  Latest build_web_compilers, move to pkg:lints, fix breaks (#605)
  13340b5  2023-01-30  dependabot[bot]  Bump dart-lang/setup-dart from 1.3 to 1.4 (#600)

package_config (https://github.com/dart-lang/package_config/compare/3fe81c4..3fe81c4):
  3fe81c4  2023-01-30  Kevin Moore  Support latest pkg:build_web_compilers, lints. Update min SDK (#129)

shelf (https://github.com/dart-lang/shelf/compare/8fca9d9..8fca9d9):
  8fca9d9  2023-01-26  Devon Carew  blast_repo fixes (#326)

string_scanner (https://github.com/dart-lang/string_scanner/compare/29e471e..29e471e):
  29e471e  2023-01-30  dependabot[bot]  Bump dart-lang/setup-dart from 1.3 to 1.4 (#53)

test (https://github.com/dart-lang/test/compare/cec47c1..cec47c1):
  cec47c1c  2023-01-27  Nate Bosch  Add missing pub requirements (#1878)
  c99d455e  2023-01-27  Nate Bosch  Prepare to publish (#1877)
  0e7ec6a7  2023-01-27  Nate Bosch  Rename `Check` to `Subject` (#1875)
  78382731  2023-01-27  Nate Bosch  Add String.matches condition (#1874)
  26e0e87b  2023-01-27  Nate Bosch  Add Iterable.containsInOrder condition (#1873)
  c9232d6b  2023-01-27  Nate Bosch  Rename `that` to `which` (#1872)
  457166b3  2023-01-26  Nate Bosch  Add missing dependency on package:lints (#1876)
  193f2a0b  2023-01-26  Nate Bosch  Retry instead of extend timeout for flaky Node tests (#1871)
  7ad9b2c3  2023-01-26  Nate Bosch  Overhaul async matchers (#1868)
  ca254546  2023-01-26  Nate Bosch  Add a withQueue utility (#1870)
  6ae2e5e9  2023-01-26  Nate Bosch  Refactor tests to a new isRejectedBy utility (#1867)
  5aeba66d  2023-01-26  Nate Bosch  Use pubspec_overrides.yaml files (#1869)

webdev (https://github.com/dart-lang/webdev/compare/ce9c581..ce9c581):
  ce9c581  2023-01-29  Anna Gringauze  Validate only needed summaries in expression_compiler_service (#1920)

Change-Id: I3ddb0ddeb3b989f6f9e78cd8aa6327aba5899018
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/280078
Commit-Queue: Devon Carew <devoncarew@google.com>
Auto-Submit: Devon Carew <devoncarew@google.com>
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants