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

unable to build doge API! #25684

Closed
shreyarastogi opened this issue Feb 4, 2020 · 7 comments
Closed

unable to build doge API! #25684

shreyarastogi opened this issue Feb 4, 2020 · 7 comments

Comments

@shreyarastogi
Copy link

@shreyarastogi shreyarastogi commented Feb 4, 2020

I am trying to implement the doge API and this is the code that I am implementing:

use crate::dom::bindings::cell::DomRefCell;
use crate::dom::bindings::codegen::Bindings::DogeBinding::{DogeMethods, DogeInit, Wrap as DogeWrap};
use crate::dom::bindings::error::{Error, Fallible};
use crate::dom::bindings::GlobalScope;
use crate::dom::bindings::root;
use crate::dom::bindings::reflector::{Reflector, reflect_dom_object};
use crate::dom::bindings::str::DOMString;
use crate::rand;
use crate::rand::Rng;
extern crate rand;

#[dom_struct]
pub struct Doge {
reflector_: Reflector,
such_list: DOMRefCell<Vec>,
}

impl Doge{

pub fn new_inherited() -> Doge {
Doge {
reflector_: Reflector::new(),
such_list: DomRefCell::new(vec![]),
}
}

pub fn new(global: &GlobalScope) -> Root {
reflect_dom_object(Box::Doge::new_inherited(), global, DogeWrap)
}

// https://jeenalee.github.io/doge-standard/#dom-doge
pub fn Constructor(global: &GlobalScope, init: Option) -> Fallible<Root> {
// Step 1
let doge = Doge::new(global);
// Step 2
if let Some(i) = init {
for word in i {
doge.Append(word);
}
}
// Step 3
Ok(doge)
}
}

impl DogeMethods for Doge{

// https://jeenalee.github.io/doge-standard/#dom-doge-append
fn Append(&self, word: DOMString) -> () {
self.such_list.borrow_mut().push(word);
}

fn Random(&self) -> Fallible {
// Step 1
let list = self.such_list.borrow();
// Step 2
if list.len() == 0 {
return Err(Error::Type("Such list is empty".to_string()));
} else {
// Step 3
let random_index = servo_rand::thread_rng().gen_range(0, list.len());
return Ok(list[random_index].clone());
}
}
}

These are the errors that I am facing:
$ ./mach build --dev
Blocking waiting for file lock on package cache
Blocking waiting for file lock on package cache
Blocking waiting for file lock on package cache
Compiling script v0.0.1 (C:\Users\shrey\servo\components\script)
warning: Error finalizing incremental compilation session directory \\?\C:\Users\shrey\servo\target\debug\incremental\canvas_traits-aj1987kq9sua\s-fkbu5d627i-159yjr0-working: Access is denied. (os error 5)

Completed script v0.0.1 custom-build (run) in 13.6s
error[E0432]: unresolved import crate::dom::bindings::GlobalScope
--> components\script\dom\doge.rs:4:5
|
4 | use crate::dom::bindings::GlobalScope;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ no GlobalScope in dom::bindings

error[E0432]: unresolved import crate::rand
--> components\script\dom\doge.rs:8:5
|
8 | use crate::rand;
| ^^^^^^^^^^^ no rand in the root

error[E0432]: unresolved import crate::rand
--> components\script\dom\doge.rs:9:12
|
9 | use crate::rand::Rng;
| ^^^^
| |
| unresolved import
| help: a similar path exists: crate::dom::doge::rand

error: cannot find attribute dom_struct in this scope
--> components\script\dom\doge.rs:12:3
|
12 | #[dom_struct]
| ^^^^^^^^^^

error[E0412]: cannot find type DOMRefCell in this scope
--> components\script\dom\doge.rs:15:16
|
15 | such_list: DOMRefCell<Vec>,
| ^^^^^^^^^^ help: a struct with a similar name exists: DomRefCell
|
::: components\script\dom\bindings\cell.rs:21:1
|
21 | / pub struct DomRefCell {
22 | | value: RefCell,
23 | | }
| |_- similarly named struct DomRefCell defined here

error[E0412]: cannot find type Root in this scope
--> components\script\dom\doge.rs:27:37
|
27 | pub fn new(global: &GlobalScope) -> Root {
| ^^^^ not found in this scope
|
help: possible candidate is found in another module, you can import it into scope
|
1 | use crate::dom::bindings::root::Root;
|

error[E0412]: cannot find type Root in this scope
--> components\script\dom\doge.rs:32:78
|
32 | pub fn Constructor(global: &GlobalScope, init: Option) -> Fallible<Root> {
| ^^^^ not found in this scope
|
help: possible candidate is found in another module, you can import it into scope
|
1 | use crate::dom::bindings::root::Root;
|

warning: unused import: crate::dom::bindings::root
--> components\script\dom\doge.rs:5:5
|
5 | use crate::dom::bindings::root;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: #[warn(unused_imports)] on by default

error[E0277]: the trait bound dom::doge::Doge: dom::bindings::reflector::DomObject is not satisfied
--> C:\Users\shrey\servo\target\debug\build\script-0fc8d3571dfd85fa\out/Bindings\DogeBinding.rs:666:1
|
666 | / pub unsafe fn Wrap(cx: SafeJSContext, scope: &GlobalScope, object: Box) -> DomRoot {
667 | | let scope = scope.reflector().get_jsobject();
668 | | assert!(!scope.get().is_null());
669 | | assert!(((*get_object_class(scope.get())).flags & JSCLASS_IS_GLOBAL) != 0);
... |
689 | | DomRoot::from_ref(&*raw)
690 | | }
| |_^ the trait dom::bindings::reflector::DomObject is not implemented for dom::doge::Doge
|
::: components\script\dom\bindings\root.rs:50:1
|
50 | pub struct Root<T: StableTraceObject> {
| ------------------------------------- required by dom::bindings::root::Root
|
= note: required because of the requirements on the impl of dom::bindings::root::StableTraceObject for dom::bindings::root::Dom<dom::doge::Doge>

error: aborting due to 8 previous errors

Some errors have detailed explanations: E0277, E0412, E0432.
For more information about an error, try rustc --explain E0277.
error: could not compile script.

To learn more, run the command again with --verbose.
Build FAILED in 0:01:26

Made some changes in the original code that has been provided in the guide. Still these errors persist. What more changes should I do?

@kunalmohan
Copy link
Collaborator

@kunalmohan kunalmohan commented Feb 5, 2020

Few corrections that should remove most of the errors:

  1. It's DomRefCell and not DOMRefCell.
  2. GlobalScope is present in components/script/dom/globalscope.rs and should be imported from there (like use crate::dom::globalscope::GlobalScope;.
  3. Remove crate:: from statements while importing rand (since its not present inside script crate).
  4. Root should be imported like use crate::dom::bindings::root::Root; as suggested in the error logs.
@shreyarastogi
Copy link
Author

@shreyarastogi shreyarastogi commented Feb 8, 2020

Hey! those inputs were really helpful, but I am still getting these errors:

shrey@LAPTOP-L08HN83K MINGW64 /c/Users/shrey/servo (master)
$ ./mach build --dev
Blocking waiting for file lock on package cache
Blocking waiting for file lock on package cache
Blocking waiting for file lock on package cache
Compiling script v0.0.1 (C:\Users\shrey\servo\components\script)
warning: Error finalizing incremental compilation session directory \\?\C:\Users\shrey\servo\target\debug\incremental\canvas_traits-aj1987kq9sua\s-fkbu5d627i-159yjr0-working: Access is denied. (os error 5)

Completed script v0.0.1 custom-build (run) in 15.9s
error: cannot find attribute dom_struct in this scope
--> components\script\dom\doge.rs:12:3
|
12 | #[dom_struct]
| ^^^^^^^^^^

error[E0277]: the trait bound dom::doge::Doge: dom::bindings::reflector::DomObject is not satisfied
--> C:\Users\shrey\servo\target\debug\build\script-0fc8d3571dfd85fa\out/Bindings\DogeBinding.rs:666:1
|
666 | / pub unsafe fn Wrap(cx: SafeJSContext, scope: &GlobalScope, object: Box) -> DomRoot {
667 | | let scope = scope.reflector().get_jsobject();
668 | | assert!(!scope.get().is_null());
669 | | assert!(((*get_object_class(scope.get())).flags & JSCLASS_IS_GLOBAL) != 0);
... |
689 | | DomRoot::from_ref(&*raw)
690 | | }
| |_^ the trait dom::bindings::reflector::DomObject is not implemented for dom::doge::Doge
|
::: components\script\dom\bindings\root.rs:50:1
|
50 | pub struct Root<T: StableTraceObject> {
| ------------------------------------- required by dom::bindings::root::Root
|
= note: required because of the requirements on the impl of dom::bindings::root::StableTraceObject for dom::bindings::root::Dom<dom::doge::Doge>

error[E0277]: the trait bound dom::doge::Doge: dom::bindings::root::StableTraceObject is not satisfied
--> components\script\dom\doge.rs:27:1
|
27 | / pub fn new(global: &GlobalScope) -> Root {
28 | | reflect_dom_object(Box::Doge::new_inherited(), global, DogeWrap)
29 | | }
| |_^ the trait dom::bindings::root::StableTraceObject is not implemented for dom::doge::Doge
|
::: components\script\dom\bindings\root.rs:50:1
|
50 | pub struct Root<T: StableTraceObject> {
| ------------------------------------- required by dom::bindings::root::Root

error[E0277]: the trait bound dom::doge::Doge: dom::bindings::root::StableTraceObject is not satisfied
--> components\script\dom\doge.rs:32:1
|
32 | / pub fn Constructor(global: &GlobalScope, init: Option) -> Fallible<Root> {
33 | | // Step 1
34 | | let doge = Doge::new(global);
35 | | // Step 2
... |
42 | | Ok(doge)
43 | | }
| |_^ the trait dom::bindings::root::StableTraceObject is not implemented for dom::doge::Doge
|
::: components\script\dom\bindings\root.rs:50:1
|
50 | pub struct Root<T: StableTraceObject> {
| ------------------------------------- required by dom::bindings::root::Root

error: aborting due to 4 previous errors

For more information about this error, try rustc --explain E0277.
error: could not compile script.

To learn more, run the command again with --verbose.
Build FAILED in 0:01:44

@kunalmohan
Copy link
Collaborator

@kunalmohan kunalmohan commented Feb 8, 2020

Ah, I missed that. You forgot to import dom_struct (use dom_struct::dom_struct;). Also, the new fn should return DomRoot<Doge> instead of Root and the Constructor should return Fallible<DomRoot<Doge>>. Don't forget to import DomRoot; present in the same file as Root.

@shreyarastogi
Copy link
Author

@shreyarastogi shreyarastogi commented Feb 8, 2020

made these changes as well, thank you. But still facing 2 more errors:

shrey@LAPTOP-L08HN83K MINGW64 /c/Users/shrey/servo (master)
$ ./mach build --dev
Blocking waiting for file lock on package cache
Blocking waiting for file lock on package cache
Blocking waiting for file lock on package cache
Compiling script v0.0.1 (C:\Users\shrey\servo\components\script)
warning: Error finalizing incremental compilation session directory \\?\C:\Users\shrey\servo\target\debug\incremental\canvas_traits-aj1987kq9sua\s-fkbu5d627i-159yjr0-working: Access is denied. (os error 5)

Completed script v0.0.1 custom-build (run) in 7.0s
error[E0433]: failed to resolve: use of undeclared type or module Trait
--> components\script\dom\doge.rs:30:47
|
30 | reflect_dom_object(<std::boxed::Box<_> as Trait>::Doge::new_inherited(), global, DogeWrap)
| ^^^^^ use of undeclared type or module Trait

error[E0053]: method Random has an incompatible type for trait
--> components\script\dom\doge.rs:55:21
|
55 | fn Random(&self) -> Fallible {
| ^^^^^^^^^^^^^^^^^^^ expected struct dom::bindings::str::DOMString, found enum std::result::Result
|
::: C:\Users\shrey\servo\target\debug\build\script-0fc8d3571dfd85fa\out/Bindings\DogeBinding.rs:707:25
|
707 | fn Random(&self) -> DOMString;
| --------- type in trait
|
= note: expected fn pointer fn(&dom::doge::Doge) -> dom::bindings::str::DOMString
found fn pointer fn(&dom::doge::Doge) -> std::result::Result<dom::bindings::str::DOMString, dom::bindings::error::Error>

error: aborting due to 2 previous errors

Some errors have detailed explanations: E0053, E0433.
For more information about an error, try rustc --explain E0053.
error: could not compile script.

To learn more, run the command again with --verbose.
Build FAILED in 0:01:34

@kunalmohan
Copy link
Collaborator

@kunalmohan kunalmohan commented Feb 8, 2020

In the new function, the first argument should be Box::new(Doge::new_inherited()). The second error occurs because according to webidl spec Random should return DOMString. Currently, you are returning Fallible which by itself doesn't make any sense. So change that to Fallible<DOMString> and in the Doge.webidl, edit the line defining Random function to [Throws] DOMString random(); (See #25376 (comment) for reference)

@shreyarastogi
Copy link
Author

@shreyarastogi shreyarastogi commented Feb 8, 2020

Build completed, thank you so much for your help!!

@jdm
Copy link
Member

@jdm jdm commented Feb 10, 2020

I'll update the original instructions to incorporate the appropriate code changes. Thanks for asking these questions @shreyarastogi, and thanks for answering them @kunalmohan!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.