Skip to content
This repository has been archived by the owner on Aug 19, 2022. It is now read-only.

Clone trait of TreeCursor might not be correctedly implemented #33

Open
Enter-tainer opened this issue Aug 26, 2021 · 5 comments
Open

Comments

@Enter-tainer
Copy link

Enter-tainer commented Aug 26, 2021

Many thanks to your awesome project!

After adding a dummy implementation to utf8_text(see silvanshade/tree-sitter-facade#34). I'm still experiencing errors.

index_bg.js:272 wasm-bindgen: imported JS function that was not marked as `catch` threw an error: memory access out of bounds

Stack:
RuntimeError: memory access out of bounds
    at http://localhost:5000/tree-sitter.wasm:wasm-function[22]:0x41ec
    at http://localhost:5000/tree-sitter.wasm:wasm-function[191]:0x2569c
    at TreeCursor.currentNode (webpack:///./node_modules/web-tree-sitter/tree-sitter.js?:4:42057)
    at eval (webpack:///./pkg/index_bg.js?:366:31)
    at logError (webpack:///./pkg/index_bg.js?:263:18)
    at Module.__wbg_currentNode_04cfbe6cd11c8027 (webpack:///./pkg/index_bg.js?:365:56)
    at __wbg_currentNode_04cfbe6cd11c8027 (http://localhost:5000/index.js:86:106)
    at web_tree_sitter_sys::TreeCursor::current_node::he854676394a58e9d (http://localhost:5000/7d4a6f82fcb2b88088ae.module.wasm:wasm-function[1074]:0x48d39)
    at tree_sitter_facade::tree_cursor::wasm::TreeCursor::node::h5ea81ee2cf927f75 (http://localhost:5000/7d4a6f82fcb2b88088ae.module.wasm:wasm-function[1156]:0x4a6de)
    at cxx2flow::parser::parse_stat::h1ee01db538459ef5 (http://localhost:5000/7d4a6f82fcb2b88088ae.module.wasm:wasm-function[37]:0x3e21)

After some debugging, I found that I have an invalid TreeCursor, which result in the failure of TreeCursor.currentNode.

https://docs.rs/web-tree-sitter-sys/0.2.6/src/web_tree_sitter_sys/lib.rs.html#626 says that TreeCursor has clone trait, but it seems that TreeCursor has no members. I'm not really familiar with wasm-bindgen. IIRC, the derived clone() function will call clone on every member of the struct. But given that TreeCursor has no members, I guess this will result in an empty clone function. Thus nothing is really cloned when calling TreeCursor::clone

@Enter-tainer
Copy link
Author

After reading wasm-bindgen's guide, I realized that Clone trait is forwarded to inner types. Maybe my error is related to other issues.

@silvanshade
Copy link
Owner

It is actually possible that this is implemented incorrectly. Looking at the tree-sitter source, it seems that there is a special ts_tree_cursor_copy function that is used to copy a TreeCursor on the C binding side of things.

I can look into this but if it's implemented incorrectly it will probably require an update to the bindings upstream to expose a proper copy method that we can wrap.

@silvanshade silvanshade reopened this Aug 31, 2021
@Enter-tainer
Copy link
Author

Many thanks! I will try to provide a minimal reproducible example.

@silvanshade
Copy link
Owner

silvanshade commented May 30, 2022

@Enter-tainer Apologies for the delay in addressing this.

I was just recently trying to expose the internal ts_tree_cursor_copy to the web bindings but it doesn't seem to be working correctly so I might have to try a different approach.

Do you happen to have a small example that demonstrates this behavior?

It would be interesting to verify that TreeCursor::clone on the native rust side works okay but that the wasm-bindgen version does not.

@Enter-tainer
Copy link
Author

I will have a try this week. Thank you for your help!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants