Skip to content

Commit

Permalink
Added Wrapped NewArrayBufferWithContents to Header (servo#484)
Browse files Browse the repository at this point in the history
Reformatted C++ Files with clang-format

Signed-off-by: Redfire <redfire75369@hotmail.com>
  • Loading branch information
Redfire75369 authored Jul 29, 2024
1 parent 5fb90b6 commit a7cfa6a
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 40 deletions.
2 changes: 1 addition & 1 deletion mozjs-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "mozjs_sys"
description = "System crate for the Mozilla SpiderMonkey JavaScript engine."
repository.workspace = true
version = "0.128.0-2"
version = "0.128.0-3"
authors = ["Mozilla"]
links = "mozjs"
build = "build.rs"
Expand Down
78 changes: 39 additions & 39 deletions mozjs-sys/src/jsapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,46 +71,47 @@ JSLinearString* AtomToLinearString(JSAtom* atom) {

// Wrappers around UniquePtr functions
/**
* Create a new ArrayBuffer with the given contents. The contents must not be
* modified by any other code, internal or external.
*
* !!! IMPORTANT !!!
* If and only if an ArrayBuffer is successfully created and returned,
* ownership of |contents| is transferred to the new ArrayBuffer.
*
* When the ArrayBuffer is ready to be disposed of, `freeFunc(contents,
* freeUserData)` will be called to release the ArrayBuffer's reference on the
* contents.
*
* `freeFunc()` must not call any JSAPI functions that could cause a garbage
* collection.
*
* The caller must keep the buffer alive until `freeFunc()` is called, or, if
* `freeFunc` is null, until the JSRuntime is destroyed.
*
* The caller must not access the buffer on other threads. The JS engine will
* not allow the buffer to be transferred to other threads. If you try to
* transfer an external ArrayBuffer to another thread, the data is copied to a
* new malloc buffer. `freeFunc()` must be threadsafe, and may be called from
* any thread.
*
* This allows ArrayBuffers to be used with embedder objects that use reference
* counting, for example. In that case the caller is responsible
* for incrementing the reference count before passing the contents to this
* function. This also allows using non-reference-counted contents that must be
* freed with some function other than free().
*/
JSObject* NewExternalArrayBuffer(
JSContext* cx, size_t nbytes, void* contents,
JS::BufferContentsFreeFunc freeFunc, void* freeUserData) {
js::UniquePtr<void, JS::BufferContentsDeleter> dataPtr{contents, {freeFunc, freeUserData}};
* Create a new ArrayBuffer with the given contents. The contents must not be
* modified by any other code, internal or external.
*
* !!! IMPORTANT !!!
* If and only if an ArrayBuffer is successfully created and returned,
* ownership of |contents| is transferred to the new ArrayBuffer.
*
* When the ArrayBuffer is ready to be disposed of, `freeFunc(contents,
* freeUserData)` will be called to release the ArrayBuffer's reference on the
* contents.
*
* `freeFunc()` must not call any JSAPI functions that could cause a garbage
* collection.
*
* The caller must keep the buffer alive until `freeFunc()` is called, or, if
* `freeFunc` is null, until the JSRuntime is destroyed.
*
* The caller must not access the buffer on other threads. The JS engine will
* not allow the buffer to be transferred to other threads. If you try to
* transfer an external ArrayBuffer to another thread, the data is copied to a
* new malloc buffer. `freeFunc()` must be threadsafe, and may be called from
* any thread.
*
* This allows ArrayBuffers to be used with embedder objects that use reference
* counting, for example. In that case the caller is responsible
* for incrementing the reference count before passing the contents to this
* function. This also allows using non-reference-counted contents that must be
* freed with some function other than free().
*/
JSObject* NewExternalArrayBuffer(JSContext* cx, size_t nbytes, void* contents,
JS::BufferContentsFreeFunc freeFunc,
void* freeUserData) {
js::UniquePtr<void, JS::BufferContentsDeleter> dataPtr{
contents, {freeFunc, freeUserData}};
return NewExternalArrayBuffer(cx, nbytes, std::move(dataPtr));
}

JSObject* NewArrayBufferWithContents(
JSContext* cx, size_t nbytes, void* contents) {
js::UniquePtr<void, JS::FreePolicy> dataPtr{contents};
return NewArrayBufferWithContents(cx, nbytes, contents);
JSObject* NewArrayBufferWithContents(JSContext* cx, size_t nbytes,
void* contents) {
js::UniquePtr<void, JS::FreePolicy> dataPtr{contents};
return NewArrayBufferWithContents(cx, nbytes, contents);
}

// Reexport some methods
Expand Down Expand Up @@ -269,8 +270,7 @@ bool CreateError(JSContext* cx, JSExnType type, JS::HandleObject stack,
JS::MutableHandleValue rval) {
return JS::CreateError(
cx, type, stack, fileName, lineNumber,
JS::ColumnNumberOneOrigin(columnNumber),
report, message,
JS::ColumnNumberOneOrigin(columnNumber), report, message,
JS::Rooted<mozilla::Maybe<JS::Value>>(cx, mozilla::ToMaybe(&cause)),
rval);
}
Expand Down
3 changes: 3 additions & 0 deletions mozjs-sys/src/jsapi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ JSObject* NewExternalArrayBuffer(
JSContext* cx, size_t nbytes, void* contents,
JS::BufferContentsFreeFunc freeFunc, void* freeUserData = nullptr);

JSObject* NewArrayBufferWithContents(
JSContext* cx, size_t nbytes, void* contents);

// Reexport some methods

bool JS_ForOfIteratorInit(
Expand Down

0 comments on commit a7cfa6a

Please sign in to comment.