Skip to content

Commit

Permalink
replace js_str with js_string
Browse files Browse the repository at this point in the history
  • Loading branch information
getong committed May 2, 2024
1 parent 3f6ee22 commit b2bc244
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 47 deletions.
8 changes: 4 additions & 4 deletions examples/src/bin/classes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use boa_engine::{
class::{Class, ClassBuilder},
error::JsNativeError,
js_str, js_string,
js_string,
native_function::NativeFunction,
property::Attribute,
Context, JsArgs, JsData, JsResult, JsString, JsValue, Source,
Expand Down Expand Up @@ -120,13 +120,13 @@ impl Class for Person {

// We add an `"inheritedProperty"` property to the prototype of `Person` with
// a value of `10` and default attribute flags `READONLY`, `NON_ENUMERABLE` and `PERMANENT`.
class.property(js_str!("inheritedProperty"), 10, Attribute::default());
class.property(js_string!("inheritedProperty"), 10, Attribute::default());

// Finally, we add a `"staticProperty"` property to `Person` with a value
// of `"Im a static property"` and attribute flags `WRITABLE`, `ENUMERABLE` and `PERMANENT`.
class.static_property(
js_str!("staticProperty"),
js_str!("Im a static property"),
js_string!("staticProperty"),
js_string!("Im a static property"),
Attribute::WRITABLE | Attribute::ENUMERABLE | Attribute::PERMANENT,
);

Expand Down
20 changes: 10 additions & 10 deletions examples/src/bin/closures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use std::cell::{Cell, RefCell};

use boa_engine::{
js_str, js_string,
js_string,
native_function::NativeFunction,
object::{builtins::JsArray, FunctionObjectBuilder, JsObject},
property::{Attribute, PropertyDescriptor},
Expand Down Expand Up @@ -52,9 +52,9 @@ fn main() -> Result<(), JsError> {
// We create a new `JsObject` with some data
let object = JsObject::with_object_proto(context.intrinsics());
object.define_property_or_throw(
js_str!("name"),
js_string!("name"),
PropertyDescriptor::builder()
.value(js_str!("Boa dev"))
.value(js_string!("Boa dev"))
.writable(false)
.enumerable(false)
.configurable(false),
Expand All @@ -77,18 +77,18 @@ fn main() -> Result<(), JsError> {
let BigStruct { greeting, object } = &mut *captures;
println!("Called `createMessage`");
// We obtain the `name` property of `captures.object`
let name = object.get(js_str!("name"), context)?;
let name = object.get(js_string!("name"), context)?;

// We create a new message from our captured variable.
let message = js_string!(
js_str!("message from `"),
&js_string!("message from `"),
&name.to_string(context)?,
js_str!("`: "),
&js_string!("`: "),
&*greeting
);

// We can also mutate the moved data inside the closure.
captures.greeting = js_string!(&*greeting, js_str!(" Hello!"));
captures.greeting = js_string!(&*greeting, &js_string!(" Hello!"));

println!("{}", message.to_std_string_escaped());
println!();
Expand All @@ -101,7 +101,7 @@ fn main() -> Result<(), JsError> {
),
)
// And here we assign `createMessage` to the `name` property of the closure.
.name(js_str!("createMessage"))
.name(js_string!("createMessage"))
// By default all `FunctionBuilder`s set the `length` property to `0` and
// the `constructable` property to `false`.
.build();
Expand All @@ -111,7 +111,7 @@ fn main() -> Result<(), JsError> {
.register_global_property(
// We set the key to access the function the same as its name for
// consistency, but it may be different if needed.
js_str!("createMessage"),
js_string!("createMessage"),
// We pass `js_function` as a property value.
js_function,
// We assign to the "createMessage" property the desired attributes.
Expand Down Expand Up @@ -144,7 +144,7 @@ fn main() -> Result<(), JsError> {
// We register a global closure that is not `Copy`.
context
.register_global_callable(
js_str!("enumerate").into(),
js_string!("enumerate"),
0,
// Note that it is required to use `unsafe` code, since the compiler cannot verify that the
// types captured by the closure are not traceable.
Expand Down
4 changes: 2 additions & 2 deletions examples/src/bin/futures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::{
use boa_engine::{
context::ContextBuilder,
job::{FutureJob, JobQueue, NativeJob},
js_str,
js_string,
native_function::NativeFunction,
property::Attribute,
Context, JsArgs, JsResult, JsValue, Source,
Expand Down Expand Up @@ -143,7 +143,7 @@ fn add_runtime(context: &mut Context) {
// Then, bind the defined async function to the ECMAScript function "delay".
context
.register_global_builtin_callable(
js_str!("delay").into(),
js_string!("delay").into(),
1,
NativeFunction::from_async_fn(delay),
)
Expand Down
11 changes: 7 additions & 4 deletions examples/src/bin/jsarray.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// This example shows how to manipulate a Javascript array using Rust code.

use boa_engine::{
js_str,
js_string,
native_function::NativeFunction,
object::{builtins::JsArray, FunctionObjectBuilder},
Context, JsResult, JsValue,
Expand All @@ -16,13 +16,16 @@ fn main() -> JsResult<()> {

assert!(array.is_empty(context)?);

array.push(js_str!("Hello, world"), context)?; // [ "Hello, world" ]
array.push(js_string!("Hello, world"), context)?; // [ "Hello, world" ]
array.push(true, context)?; // [ "Hello, world", true ]

assert!(!array.is_empty(context)?);

assert_eq!(array.pop(context)?, JsValue::new(true)); // [ "Hello, world" ]
assert_eq!(array.pop(context)?, JsValue::new(js_str!("Hello, world"))); // [ ]
assert_eq!(
array.pop(context)?,
JsValue::new(js_string!("Hello, world"))
); // [ ]
assert_eq!(array.pop(context)?, JsValue::undefined()); // [ ]

array.push(1, context)?; // [ 1 ]
Expand Down Expand Up @@ -113,7 +116,7 @@ fn main() -> JsResult<()> {

context
.global_object()
.set(js_str!("myArray"), array, true, context)?;
.set(js_string!("myArray"), array, true, context)?;

Ok(())
}
4 changes: 2 additions & 2 deletions examples/src/bin/jsarraybuffer.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// This example shows how to manipulate a Javascript array using Rust code.

use boa_engine::{
js_str,
js_string,
object::builtins::{JsArrayBuffer, JsDataView, JsUint32Array, JsUint8Array},
property::Attribute,
Context, JsResult, JsValue,
Expand Down Expand Up @@ -54,7 +54,7 @@ fn main() -> JsResult<()> {
// We can also register it as a global property
context
.register_global_property(
js_str!("myArrayBuffer"),
js_string!("myArrayBuffer"),
array_buffer,
Attribute::WRITABLE | Attribute::ENUMERABLE | Attribute::CONFIGURABLE,
)
Expand Down
12 changes: 6 additions & 6 deletions examples/src/bin/jstypedarray.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// This example shows how to manipulate a Javascript array using Rust code.

use boa_engine::{
js_str,
js_string,
native_function::NativeFunction,
object::{
builtins::{JsArray, JsArrayBuffer, JsUint8Array},
Expand Down Expand Up @@ -168,7 +168,7 @@ fn main() -> JsResult<()> {
.buffer(context)?
.as_object()
.unwrap()
.get(js_str!("byteLength"), context)
.get(js_string!("byteLength"), context)
.unwrap(),
JsValue::new(8)
);
Expand All @@ -195,23 +195,23 @@ fn main() -> JsResult<()> {

// toLocaleString
// let array = JsUint32Array::from_iter(vec![500, 8123, 12], context)?;
// let locales: Option<JsValue> = Some(js_str!("de-DE").into());
// let locales: Option<JsValue> = Some(js_string!("de-DE").into());
// let options = Some(context.eval(Source::from_bytes(
// r##"let options = { style: "currency", currency: "EUR" }; options;"##,
// ))?);
// assert_eq!(
// array.to_locale_string(locales, options, context)?,
// js_str!("500,00 €,8.123,00 €,12,00 €").into()
// js_string!("500,00 €,8.123,00 €,12,00 €").into()
// );

// toStringTag
let array = JsUint8Array::from_iter(vec![1u8, 2u8, 3u8, 4u8, 5u8, 6u8, 7u8, 8u8], context)?;
let tag = array.to_string_tag(context)?.to_string(context)?;
assert_eq!(tag, js_str!("Uint8Array"));
assert_eq!(tag, js_string!("Uint8Array"));

context
.register_global_property(
js_str!("myUint8Array"),
js_string!("myUint8Array"),
array,
Attribute::WRITABLE | Attribute::ENUMERABLE | Attribute::CONFIGURABLE,
)
Expand Down
10 changes: 6 additions & 4 deletions examples/src/bin/module_fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::{
use boa_engine::{
builtins::promise::PromiseState,
job::{FutureJob, JobQueue, NativeJob},
js_str,
js_string,
module::ModuleLoader,
Context, JsNativeError, JsResult, JsString, JsValue, Module,
};
Expand Down Expand Up @@ -148,7 +148,9 @@ fn main() -> JsResult<()> {
}
}

let default = module.namespace(context).get(js_str!("default"), context)?;
let default = module
.namespace(context)
.get(js_string!("default"), context)?;

// `default` should contain the result of our calculations.
let default = default
Expand All @@ -160,14 +162,14 @@ fn main() -> JsResult<()> {
.get(0, context)?
.as_string()
.ok_or_else(|| JsNativeError::typ().with_message("array element was not a string"))?,
&js_str!("aGVsbG8=")
&js_string!("aGVsbG8=")
);
assert_eq!(
default
.get(1, context)?
.as_string()
.ok_or_else(|| JsNativeError::typ().with_message("array element was not a string"))?,
&js_str!("d29ybGQ=")
&js_string!("d29ybGQ=")
);

Ok(())
Expand Down
12 changes: 6 additions & 6 deletions examples/src/bin/modulehandler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// the require/module.exports pattern

use boa_engine::{
js_str, native_function::NativeFunction, prelude::JsObject, property::Attribute, Context,
js_string, native_function::NativeFunction, prelude::JsObject, property::Attribute, Context,
JsArgs, JsNativeError, JsResult, JsValue, Source,
};
use boa_runtime::Console;
Expand Down Expand Up @@ -33,14 +33,14 @@ fn main() -> Result<(), Box<dyn Error>> {
// Adding custom object that mimics 'module.exports'
let moduleobj = JsObject::default();
moduleobj.set(
js_str!("exports"),
JsValue::from(js_str!(" ")),
js_string!("exports"),
JsValue::from(js_string!(" ")),
false,
&mut ctx,
)?;

ctx.register_global_property(
js_str!("module"),
js_string!("module"),
JsValue::from(moduleobj),
Attribute::default(),
)?;
Expand Down Expand Up @@ -68,9 +68,9 @@ fn require(_: &JsValue, args: &[JsValue], ctx: &mut Context) -> JsResult<JsValue

// Access module.exports and return as ResultValue
let global_obj = ctx.global_object();
let module = global_obj.get(js_str!("module"), ctx)?;
let module = global_obj.get(js_string!("module"), ctx)?;
module
.as_object()
.ok_or_else(|| JsNativeError::typ().with_message("`exports` property was not an object"))?
.get(js_str!("exports"), ctx)
.get(js_string!("exports"), ctx)
}
11 changes: 7 additions & 4 deletions examples/src/bin/modules.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{error::Error, path::Path, rc::Rc};

use boa_engine::{
builtins::promise::PromiseState, js_str, module::SimpleModuleLoader, Context, JsError,
builtins::promise::PromiseState, js_string, module::SimpleModuleLoader, Context, JsError,
JsNativeError, JsValue, Module, NativeFunction,
};
use boa_parser::Source;
Expand Down Expand Up @@ -102,14 +102,17 @@ fn main() -> Result<(), Box<dyn Error>> {

// We can access the full namespace of the module with all its exports.
let namespace = module.namespace(context);
let result = namespace.get(js_str!("result"), context)?;
let result = namespace.get(js_string!("result"), context)?;

println!("result = {}", result.display());

assert_eq!(namespace.get(js_str!("result"), context)?, JsValue::from(5));
assert_eq!(
namespace.get(js_string!("result"), context)?,
JsValue::from(5)
);

let mix = namespace
.get(js_str!("mix"), context)?
.get(js_string!("mix"), context)?
.as_callable()
.cloned()
.ok_or_else(|| JsNativeError::typ().with_message("mix export wasn't a function!"))?;
Expand Down
12 changes: 7 additions & 5 deletions examples/src/bin/synthetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ use boa_engine::builtins::promise::PromiseState;
use boa_engine::module::{SimpleModuleLoader, SyntheticModuleInitializer};
use boa_engine::object::FunctionObjectBuilder;
use boa_engine::{
js_str, js_string, Context, JsArgs, JsError, JsNativeError, JsValue, Module, NativeFunction,
Source,
js_string, Context, JsArgs, JsError, JsNativeError, JsValue, Module, NativeFunction, Source,
};

fn main() -> Result<(), Box<dyn Error>> {
Expand Down Expand Up @@ -78,14 +77,17 @@ fn main() -> Result<(), Box<dyn Error>> {

// We can access the full namespace of the module with all its exports.
let namespace = module.namespace(context);
let result = namespace.get(js_str!("result"), context)?;
let result = namespace.get(js_string!("result"), context)?;

println!("result = {}", result.display());

assert_eq!(namespace.get(js_str!("result"), context)?, JsValue::from(5));
assert_eq!(
namespace.get(js_string!("result"), context)?,
JsValue::from(5)
);

let mix = namespace
.get(js_str!("mix"), context)?
.get(js_string!("mix"), context)?
.as_callable()
.cloned()
.ok_or_else(|| JsNativeError::typ().with_message("mix export wasn't a function!"))?;
Expand Down

0 comments on commit b2bc244

Please sign in to comment.