-
-
Notifications
You must be signed in to change notification settings - Fork 411
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
163 changed files
with
4,274 additions
and
3,090 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
use boa_engine::{ | ||
js_str, js_string, object::ObjectInitializer, property::Attribute, string::JsStrVariant, | ||
Context, JsNativeError, JsObject, JsResult, JsValue, NativeFunction, | ||
}; | ||
|
||
fn storage(_: &JsValue, args: &[JsValue], _: &mut Context) -> JsResult<JsValue> { | ||
let Some(value) = args.first() else { | ||
return Err(JsNativeError::typ() | ||
.with_message("expected string argument") | ||
.into()); | ||
}; | ||
|
||
let Some(string) = value.as_string() else { | ||
return Err(JsNativeError::typ() | ||
.with_message(format!("expected string, got {}", value.type_of())) | ||
.into()); | ||
}; | ||
|
||
let storage = if string.is_static() { "static" } else { "heap" }; | ||
Ok(js_string!(storage).into()) | ||
} | ||
|
||
fn encoding(_: &JsValue, args: &[JsValue], _: &mut Context) -> JsResult<JsValue> { | ||
let Some(value) = args.first() else { | ||
return Err(JsNativeError::typ() | ||
.with_message("expected string argument") | ||
.into()); | ||
}; | ||
|
||
let Some(string) = value.as_string() else { | ||
return Err(JsNativeError::typ() | ||
.with_message(format!("expected string, got {}", value.type_of())) | ||
.into()); | ||
}; | ||
|
||
let str = string.as_str(); | ||
let encoding = match str.variant() { | ||
JsStrVariant::Latin1(_) => "latin1", | ||
JsStrVariant::Utf16(_) => "utf16", | ||
}; | ||
Ok(js_string!(encoding).into()) | ||
} | ||
|
||
fn summary(_: &JsValue, args: &[JsValue], context: &mut Context) -> JsResult<JsValue> { | ||
let Some(value) = args.first() else { | ||
return Err(JsNativeError::typ() | ||
.with_message("expected string argument") | ||
.into()); | ||
}; | ||
|
||
let Some(string) = value.as_string() else { | ||
return Err(JsNativeError::typ() | ||
.with_message(format!("expected string, got {}", value.type_of())) | ||
.into()); | ||
}; | ||
|
||
let storage = if string.is_static() { "static" } else { "heap" }; | ||
let encoding = match string.as_str().variant() { | ||
JsStrVariant::Latin1(_) => "latin1", | ||
JsStrVariant::Utf16(_) => "utf16", | ||
}; | ||
|
||
let summary = ObjectInitializer::new(context) | ||
.property(js_str!("storage"), js_string!(storage), Attribute::all()) | ||
.property(js_str!("encoding"), js_string!(encoding), Attribute::all()) | ||
.build(); | ||
|
||
Ok(summary.into()) | ||
} | ||
|
||
pub(super) fn create_string(context: &mut Context) -> JsObject { | ||
ObjectInitializer::new(context) | ||
.function(NativeFunction::from_fn_ptr(storage), js_str!("storage"), 1) | ||
.function( | ||
NativeFunction::from_fn_ptr(encoding), | ||
js_str!("encoding"), | ||
1, | ||
) | ||
.function(NativeFunction::from_fn_ptr(summary), js_str!("summary"), 1) | ||
.build() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.