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

Exposing Value::TypeOf #1133

Merged
merged 6 commits into from Nov 27, 2022
Merged

Exposing Value::TypeOf #1133

merged 6 commits into from Nov 27, 2022

Conversation

ghost
Copy link

@ghost ghost commented Nov 25, 2022

@CLAassistant
Copy link

CLAassistant commented Nov 25, 2022

CLA assistant check
All committers have signed the CLA.

Copy link
Member

@bartlomieju bartlomieju left a comment

Choose a reason for hiding this comment

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

Cool, can you add a test somewhere in test_api.rs comparing types of two strings (to assert that it matches) and then a string and a number?

@ghost
Copy link
Author

ghost commented Nov 26, 2022

I can have a go at it.

@ghost
Copy link
Author

ghost commented Nov 26, 2022

Excuse terminology or if my understanding is incorrect. As I would like to work through this. What I just realized this morning is that I did not bind v8__Value__TypeOf in bindings.cc. However if I bind this function in bindings.cc. I still receive an undefined reference to the function itself.

const v8::String v8__Value__TypeOf(const v8::Value& self, v8::Isolate* isolate) {
  return self.TypeOf(isolate);
}
note: /usr/bin/ld: /root/Deno/rusty_v8/target/debug/deps/test_api-f7990818cf20050a.4svfdgqfs3t2c6tr.rcgu.o: in function `v8::value::<impl v8::data::Value>::type_of':
          /root/Deno/rusty_v8/src/value.rs:709: undefined reference to `v8__Value__TypeOf'
          collect2: error: ld returned 1 exit status

  = help: some `extern` functions couldn't be found; some native libraries may need to be installed or have their path specified
}

@littledivy
Copy link
Member

To build with changes in bindings.cc you need to set V8_FROM_SOURCE=1

V8_FROM_SOURCE=1 cargo build

@ghost
Copy link
Author

ghost commented Nov 26, 2022

I will continue troubleshooting later today there are tons of examples on how to test. I suppose that makes sense how can I pass an independent copy of V8 as a parameter and or expect to use to the newly implemented function if the source hasn't been compiled locally on my machine instead of using the prebuilt version using cargo build

src/value.rs Outdated
@@ -142,6 +142,7 @@ extern "C" {
fn v8__Value__BooleanValue(this: *const Value, isolate: *mut Isolate)
-> bool;
fn v8__Value__GetHash(this: *const Value) -> int;
fn v8__Value__TypeOf(this: *const Value, isolate: *mut Isolate) -> String;
Copy link
Member

Choose a reason for hiding this comment

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

*const String - V8 String is a reference not an owned String

Copy link
Author

Choose a reason for hiding this comment

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

So my original commit was correct in passing the Isolate as a mutable reference?

Copy link
Member

Choose a reason for hiding this comment

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

No - this is correct you need to pass raw pointers between Rust and C++

@ghost
Copy link
Author

ghost commented Nov 26, 2022

I was having some memory issues with WSL2 on Ubuntu 22.04 and 20.04 on Windows 11 while compiling on this tiny laptop. So Powershell for everything is it these days. I can atleast compile this with sccache installed within 15-20 seconds.

If I was reading the docs correctly Local<String> should be the return type on TypeOf. What am I missing here since I am stating that I want to call C directly?

v8::Local<v8::String> v8__Value__TypeOf(v8::Value& self, v8::Isolate* isolate) {
  return self.TypeOf(isolate);
}
../../../src/binding.cc(762,23): error: 'v8__Value__TypeOf' has C-linkage specified, but returns user-defined type 'v8::Local<v8::String>' which is incompatible with C [-Werror,-Wreturn-type-c-linkage]
  v8::Local<v8::String> v8__Value__TypeOf(v8::Value& self, v8::Isolate* isolate) {

@bartlomieju
Copy link
Member

@WingZer0o take a look at other APIs that return the same type - eg. v8__StackFrame__GetScriptNameOrSourceURL. You will need to call local_to_ptr on the thing you want to return and specify type as const v8::String *. Then on Rust side you will need to map that type back to v8::Local using scope.cast_local()

@ghost
Copy link
Author

ghost commented Nov 26, 2022

Included my test case for this PR. Please let know if anything needs to be changed if I messed up. This was a good issue for me to work on to solidify some knowledge I had from Node.js conceptually not practically. I've always wanted to get involved in either project considering all this browser work that I do and my old TypeScript projects, have to dive in I guess.

@bartlomieju
Copy link
Member

bartlomieju commented Nov 26, 2022

Included my test case for this PR. Please let know if anything needs to be changed if I messed up. This was a good issue for me to work on to solidify some knowledge I had from Node.js conceptually not practically. I've always wanted to get involved in either project considering all this browser work that I do and my old TypeScript projects, have to dive in I guess.

@WingZer0o this is a great first contribution! It looks good to me, please make sure to run cargo fmt to make CI happy on formatting. (If that doesn't work do rustfmt ./src/lib.rs and rustfmt ./tests/test_api.rs)

@ghost
Copy link
Author

ghost commented Nov 27, 2022

Looks like the fixed a few formatting errors.

Copy link
Member

@bartlomieju bartlomieju left a comment

Choose a reason for hiding this comment

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

LGTM, thanks @WingZer0o!

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.

None yet

4 participants