1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.

use deno_core::crate_modules;
use std::path::PathBuf;

crate_modules!();

pub struct WebScripts {
  pub abort_signal: String,
  pub declaration: String,
  pub dom_exception: String,
  pub event: String,
  pub text_encoding: String,
}

fn get_str_path(file_name: &str) -> String {
  PathBuf::from(DENO_CRATE_PATH)
    .join(file_name)
    .to_string_lossy()
    .to_string()
}

pub fn get_scripts() -> WebScripts {
  WebScripts {
    abort_signal: get_str_path("02_abort_signal.js"),
    declaration: get_str_path("lib.deno_web.d.ts"),
    dom_exception: get_str_path("00_dom_exception.js"),
    event: get_str_path("01_event.js"),
    text_encoding: get_str_path("08_text_encoding.js"),
  }
}

#[cfg(test)]
mod tests {
  use deno_core::js_check;
  use deno_core::CoreIsolate;
  use deno_core::StartupData;
  use futures::future::lazy;
  use futures::future::FutureExt;
  use futures::task::Context;
  use futures::task::Poll;

  fn run_in_task<F>(f: F)
  where
    F: FnOnce(&mut Context) + Send + 'static,
  {
    futures::executor::block_on(lazy(move |cx| f(cx)));
  }

  fn setup() -> CoreIsolate {
    let mut isolate = CoreIsolate::new(StartupData::None, false);
    js_check(
      isolate
        .execute("00_dom_exception.js", include_str!("00_dom_exception.js")),
    );
    js_check(isolate.execute("01_event.js", include_str!("01_event.js")));
    js_check(
      isolate.execute("02_abort_signal.js", include_str!("02_abort_signal.js")),
    );
    js_check(
      isolate
        .execute("08_text_encoding.js", include_str!("08_text_encoding.js")),
    );
    isolate
  }

  #[test]
  fn test_abort_controller() {
    run_in_task(|mut cx| {
      let mut isolate = setup();
      js_check(isolate.execute(
        "abort_controller_test.js",
        include_str!("abort_controller_test.js"),
      ));
      if let Poll::Ready(Err(_)) = isolate.poll_unpin(&mut cx) {
        unreachable!();
      }
    });
  }

  #[test]
  fn test_event() {
    run_in_task(|mut cx| {
      let mut isolate = setup();
      js_check(isolate.execute("event_test.js", include_str!("event_test.js")));
      if let Poll::Ready(Err(_)) = isolate.poll_unpin(&mut cx) {
        unreachable!();
      }
    });
  }

  #[test]
  fn test_event_target() {
    run_in_task(|mut cx| {
      let mut isolate = setup();
      js_check(
        isolate.execute(
          "event_target_test.js",
          include_str!("event_target_test.js"),
        ),
      );
      if let Poll::Ready(Err(_)) = isolate.poll_unpin(&mut cx) {
        unreachable!();
      }
    });
  }

  #[test]
  fn test_text_encoding() {
    run_in_task(|mut cx| {
      let mut isolate = setup();
      js_check(isolate.execute(
        "text_encoding_test.js",
        include_str!("text_encoding_test.js"),
      ));
      if let Poll::Ready(Err(_)) = isolate.poll_unpin(&mut cx) {
        unreachable!();
      }
    });
  }
}