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

feat: add Object registry for foreign objects and binary_tree module #12

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

mohammad-shahabadi
Copy link
Contributor

@mohammad-shahabadi mohammad-shahabadi commented Nov 17, 2024

Features:

  • Added an interface for foreign objects from modules called objectReferenceRegistry.
  • We store each foreign object returned from modules inside this entity.
  • Moved freeList and allocatedBlocks initialization from each module to module repository. Now each module share the same objects of freeList and allocatedBlocks.
  • Each foreign object stored has unique identification to recognize when returned from modules and vice versa.
  • Added binary_tree module with all of its functionalities.
  • Now free function can be used as to free foreign objects from memory.
  • If a returned foreign object was null, address 0 will be returned as a void pointer. Address 0 is considered as a null pointer and it is being used to recognize null objects in binary_tree.

ATTENTION:
This PL should be merged after #10 and #11 .
If needed, rebase it first with main branch then merge it.

Tests:
All of the tests are passed.

@mohammad-shahabadi mohammad-shahabadi changed the title Feat/js object registry feat: add Object registry for foreign objects and binary_tree module Nov 17, 2024
Copy link
Contributor

@benmurphyy benmurphyy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be good to add some tests to the tests folder too!

parameters: [
{
type: "primary",
primaryDataType: "signed int"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do specify somewhere in the docs that this defers from normal make_tree source academy module as you are only able to take in signed ints as your node value, whereas the module itself could have any JS type

}
const objIdentifier = new Uint8Array([
(FOREIGN_OBJ_IDENTIFIER >> 8) & 0xFF, // High byte (0xF0)
FOREIGN_OBJ_IDENTIFIER & 0xFF // Low byte (0xBA)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is the purpose of FOREIGN_OBJ_IDENTIFIER ? it does not seem to be used anywhere. It seems you're just using it as a placeholder value for the address that you give your foreign object for the value of its void *. In that case you could just leave it as zeros since from the C code deferencing the void * would be UB. Unless you need it on the JS side, which dosent seem to be the case in this PR.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively the C standard does not specify that the value of a pointer must be an actual address in memory, merely a "reference". In that case you could instead come up with a solution to giving a unique value for your foreign object void * variables which are guaranteed to not be equal to any other foreign object or actual C object or NULL. To do this, you could reserve a portion of the addressable memory range beforehand and assign foreign object void * values from there, or even just increase the underlying primitive type of pointers from unsigned int to unsigned long int (32bit to 64bit), while the WASM addressable space is still 32bit (limitation of WASM memory addressing AFAIK). You could modify this in c-slang/ctowasm/src/common/constants.ts (might be some other changes needed too). That way you can just assign your foreign object void * values from outside the unsigned 32bit numeric range.

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

Successfully merging this pull request may close these issues.

2 participants