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

[ruby] Class & Singleton Class Modelling #4652

Closed
DavidBakerEffendi opened this issue Jun 10, 2024 · 0 comments · Fixed by #4655
Closed

[ruby] Class & Singleton Class Modelling #4652

DavidBakerEffendi opened this issue Jun 10, 2024 · 0 comments · Fixed by #4655
Assignees
Labels
ruby Relates to rubysrc2cpg

Comments

@DavidBakerEffendi
Copy link
Collaborator

DavidBakerEffendi commented Jun 10, 2024

Ruby, similar to Python, has the notion of a "meta-class" called the singleton class. This can be seen in methods such as def self.foo;end and def foo;end where the former is defined against the singleton. Similarly in fields, where @@ fields are defined against the singleton class.

When instantiating classes, Foo.new is a call to the singleton class that allocates memory and calls the respective class' initialize method.

To model this, we need:

  1. To create classes and modules in pairs, where one is Foo (the "regular" class) and the other is Foo<class> (the singleton class)
  2. Define members for @@ fields under Foo<class>
  3. Define members with dynamicTypeHintFullNames set to the corresponding self methods and under Foo<class>. Foo<class> should have empty bindings to these self methods. An empty binding is one with name=="" and signature==""
  4. During object instantiations, e.g. Foo.new, add Foo<class> to the receiver's dynamic type hints, and Foo as the return type of Call(new). The call linker may add an edge directly from Foo.new to Foo.initialize.
  5. Figure out Ruby field instantiation method names, similar to Java's <clinit> and use more Ruby-specific names for the constructor cycle.
@DavidBakerEffendi DavidBakerEffendi added the ruby Relates to rubysrc2cpg label Jun 10, 2024
@DavidBakerEffendi DavidBakerEffendi self-assigned this Jun 10, 2024
DavidBakerEffendi added a commit that referenced this issue Jun 11, 2024
1. Created classes and modules in pairs, where one is `Foo` (the "regular" class) and the other is `Foo<class>` (the singleton class). `module` singletons get the `final` keyword.
2. Define members for `@@` fields under `Foo<class>`
3. Define members with `dynamicTypeHintFullName`s set to the corresponding `self` methods and under `Foo<class>`. `Foo<class>` should have empty bindings to these `self` methods. An empty binding is one with `name==""` and `signature==""`
4. During object instantiations, e.g. `Foo.new`, add `Foo<class>` to the receiver's dynamic type hints, and `Foo` as the return type of `Call(new)`. The call linker may add an edge directly from `Foo.new` to `Foo.initialize`.

Resolves #4652
DavidBakerEffendi added a commit that referenced this issue Jun 12, 2024
1. Created classes and modules in pairs, where one is `Foo` (the "regular" class) and the other is `Foo<class>` (the singleton class). `module` singletons get the `final` keyword.
2. Define members for `@@` fields under `Foo<class>`
3. Define members with `dynamicTypeHintFullName`s set to the corresponding `self` methods and under `Foo<class>`. `Foo<class>` should have empty bindings to these `self` methods. An empty binding is one with `name==""` and `signature==""`
4. During object instantiations, e.g. `Foo.new`, add `Foo<class>` to the receiver's dynamic type hints, and `Foo` as the return type of `Call(new)`. The call linker may add an edge directly from `Foo.new` to `Foo.initialize`.

cc @AndreiDreyer 

Resolves #4652
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ruby Relates to rubysrc2cpg
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant