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

Add console support to web-sys #450

Closed
2 tasks
fitzgen opened this issue Jul 10, 2018 · 13 comments
Closed
2 tasks

Add console support to web-sys #450

fitzgen opened this issue Jul 10, 2018 · 13 comments
Labels
help wanted We could use some help fixing this issue! web-sys Issues related to the `web-sys` crate

Comments

@fitzgen
Copy link
Member

fitzgen commented Jul 10, 2018

This is a tracking issue for adding support for the console API to the web-sys crate.

General info on the console API: https://developer.mozilla.org/en-US/docs/Web/API/Console

How to add support for new Web APIs to web-sys: https://rustwasm.github.io/wasm-bindgen/web-sys/supporting-more-web-apis.html

Interfaces

Here is a list of interfaces we need to support:

  • Console

Example

  • Modify the existing console logging example in examples/ to use web-sys
@fitzgen fitzgen added help wanted We could use some help fixing this issue! web-sys Issues related to the `web-sys` crate labels Jul 10, 2018
@jannikkeye
Copy link
Contributor

Building web-sys after moving the Console webidl file to enabled results in the following console output. I wasn't able to figure out if its an error in the file or the parser or the web-sys crate.

--- stdout
cargo:rerun-if-changed=build.rs
cargo:rerun-if-changed=webidls/enabled
cargo:rerun-if-changed=webidls/enabled/StorageType.webidl
cargo:rerun-if-changed=webidls/enabled/DOMHighResTimeStamp.webidl
cargo:rerun-if-changed=webidls/enabled/Console.webidl
cargo:rerun-if-changed=webidls/enabled/Response.webidl
cargo:rerun-if-changed=webidls/enabled/Event.webidl

--- stderr
Error: compiling WebIDL into wasm-bindgen bindings
  caused by parsing WebIDL source text
  caused by Unrecognized token `Const` found at 2861:2866
Expected one of "(", "ArrayBuffer", "ByteString", "DOMString", "DataView", "Error", "Float32Array", "Float64Array", "FrozenArray", "Identifier", "Int16Array", "Int32Array", "Int8Array", "Promise", "USVString", "Uint16Array", "Uint32Array", "Uint8Array", "Uint8ClampedArray", "any", "boolean", "byte", "double", "float", "long", "object", "octet", "readonly", "record", "sequence", "short", "symbol", "unrestricted", "unsigned" or "void"

@fitzgen
Copy link
Member Author

fitzgen commented Jul 11, 2018

@jannikkeye, can you minimize the test case and file a bug upstream?

@migerh
Copy link

migerh commented Jul 11, 2018

I'm not entirely sure since I'm rather new to webidl but it seems that const can only appear in an interface: https://heycam.github.io/webidl/#idl-constants

But in console.webidl it appears inside the namespace console:

namespace console {
// NOTE: if you touch this namespace, remember to update the ConsoleInstance
// interface as well!
// Logging
[UseCounter]
void assert(optional boolean condition = false, any... data);
[UseCounter]
void clear();
[UseCounter]
void count(optional DOMString label = "default");
[UseCounter]
void countReset(optional DOMString label = "default");
[UseCounter]
void debug(any... data);
[UseCounter]
void error(any... data);
[UseCounter]
void info(any... data);
[UseCounter]
void log(any... data);
[UseCounter]
void table(any... data); // FIXME: The spec is still unclear about this.
[UseCounter]
void trace(any... data);
[UseCounter]
void warn(any... data);
[UseCounter]
void dir(any... data); // FIXME: This doesn't follow the spec yet.
[UseCounter]
void dirxml(any... data);
// Grouping
[UseCounter]
void group(any... data);
[UseCounter]
void groupCollapsed(any... data);
[UseCounter]
void groupEnd();
// Timing
[UseCounter]
void time(optional DOMString label = "default");
[UseCounter]
void timeLog(optional DOMString label = "default", any... data);
[UseCounter]
void timeEnd(optional DOMString label = "default");
// Mozilla only or Webcompat methods
[UseCounter]
void _exception(any... data);
[UseCounter]
void timeStamp(optional any data);
[UseCounter]
void profile(any... data);
[UseCounter]
void profileEnd(any... data);
[ChromeOnly]
const boolean IS_NATIVE_CONSOLE = true;

@migerh
Copy link

migerh commented Jul 11, 2018

In case I'm wrong, here's a minimal example to reproduce the issue with webidl-rs:

[Exposed=(Window)]
namespace console {
    const boolean IS_NATIVE_CONSOLE = true;
};

@fitzgen
Copy link
Member Author

fitzgen commented Jul 11, 2018

Two thoughts:

  1. This particular constant is ChromeOnly, so I think we can safely comment it out and get on with our lives.

  2. Might be worth filing that issue upstream with both the webidl crate and the spec anyways.

@richard-uk1
Copy link
Contributor

If you comment out the incorrect const, then it builds, but you still don't have access to console functions because they are defined like this:

[Exposed=(Window,Worker,WorkerDebugger,Worklet,System),
 ClassString="Console",
 ProtoObjectHack]
namespace console {
// ...

which I'm guessing means "these are exposed on the (window/worker/...) prototype as console, which has class name Console.

So should web-sys be something like

#[wasm_bindgen]
extern "C" {
    #[wasm_bindgen(js_namespace = console)]
    fn log(s: &str); // not the correct type
}

and then, how do you stop name collisions from functions with the same name in different namespaces? Should web-sys generate modules to match namespaces?

@migerh
Copy link

migerh commented Jul 15, 2018

@derekdreery That's basically what is suggested in #253. I think getting webidl namespace support first would be very helpful to get the Console.webidl up and running properly.

@richard-uk1
Copy link
Contributor

Thanks for linking the issue :)

@richard-uk1
Copy link
Contributor

Brain dump:
Rather than being like arbitrarily nested rust modules, a WebIDL namespace is like a global singleton object with methods. So console is like someone has done var console = new Console() at the beginning of each page. So we can still use modules from rust, but we should note the semantic differences.

@eminence
Copy link
Contributor

Now that #678 has landed, what's left to do for this issue?

@migerh
Copy link

migerh commented Sep 1, 2018

@eminence If I'm not completely mistaken, generating variadic functions like log and warn are not yet supported. See also #503.

@richard-uk1
Copy link
Contributor

@migerh @eminence I'm work towards adding variadic support in #726. After that lands, I'll modify the webidl frontend to emit variadic functions (where the last arg is a slice/vec). There's also variadic the other way, as in passing a rust callback that is called variadically by javascript. I think we need this to support some callbacks, e.g. for events.

@fitzgen
Copy link
Member Author

fitzgen commented Sep 12, 2018

@fitzgen fitzgen closed this as completed Sep 12, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted We could use some help fixing this issue! web-sys Issues related to the `web-sys` crate
Projects
None yet
Development

No branches or pull requests

5 participants