2019-10-23 09:26:32 -07:00
|
|
|
// Copyright (c) 2019 GitHub, Inc.
|
|
|
|
// Use of this source code is governed by the MIT license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2021-11-22 08:34:31 +01:00
|
|
|
#ifndef ELECTRON_SHELL_COMMON_NODE_UTIL_H_
|
|
|
|
#define ELECTRON_SHELL_COMMON_NODE_UTIL_H_
|
2019-10-23 09:26:32 -07:00
|
|
|
|
2025-01-23 08:55:28 -06:00
|
|
|
#include <string>
|
2024-09-16 15:53:04 -05:00
|
|
|
#include <string_view>
|
2019-10-23 09:26:32 -07:00
|
|
|
#include <vector>
|
|
|
|
|
2024-10-10 08:34:55 -05:00
|
|
|
#include "base/containers/span.h"
|
2025-05-07 19:21:39 -06:00
|
|
|
#include "base/memory/raw_ptr.h"
|
|
|
|
#include "v8-microtask-queue.h"
|
2024-07-29 12:42:57 -05:00
|
|
|
#include "v8/include/v8-forward.h"
|
2019-10-23 09:26:32 -07:00
|
|
|
|
|
|
|
namespace node {
|
|
|
|
class Environment;
|
2025-01-23 08:55:28 -06:00
|
|
|
class IsolateData;
|
|
|
|
struct ThreadId;
|
|
|
|
|
|
|
|
namespace EnvironmentFlags {
|
|
|
|
enum Flags : uint64_t;
|
2021-07-01 17:51:37 -07:00
|
|
|
}
|
2025-01-23 08:55:28 -06:00
|
|
|
} // namespace node
|
2019-10-23 09:26:32 -07:00
|
|
|
|
2022-06-29 12:55:47 -07:00
|
|
|
namespace electron::util {
|
2019-10-23 09:26:32 -07:00
|
|
|
|
2024-09-16 15:53:04 -05:00
|
|
|
// Emit a warning via node's process.emitWarning()
|
|
|
|
void EmitWarning(v8::Isolate* isolate,
|
|
|
|
std::string_view warning_msg,
|
|
|
|
std::string_view warning_type);
|
|
|
|
|
|
|
|
// Emit a warning via node's process.emitWarning(),
|
2025-04-30 11:48:35 -07:00
|
|
|
// using JavascriptEnvironment's isolate
|
2024-09-16 15:53:04 -05:00
|
|
|
void EmitWarning(std::string_view warning_msg, std::string_view warning_type);
|
|
|
|
|
2025-04-30 11:48:35 -07:00
|
|
|
// Emit a deprecation warning via node's process.emitWarning()
|
|
|
|
void EmitDeprecationWarning(v8::Isolate* isolate,
|
|
|
|
std::string_view warning_msg,
|
|
|
|
std::string_view deprecation_code = "");
|
|
|
|
|
|
|
|
// Emit a deprecation warning via node's process.emitWarning(),
|
|
|
|
// using JavascriptEnvironment's isolate
|
|
|
|
void EmitDeprecationWarning(std::string_view warning_msg,
|
|
|
|
std::string_view deprecation_code = "");
|
|
|
|
|
2019-10-23 09:26:32 -07:00
|
|
|
// Run a script with JS source bundled inside the binary as if it's wrapped
|
|
|
|
// in a function called with a null receiver and arguments specified in C++.
|
|
|
|
// The returned value is empty if an exception is encountered.
|
|
|
|
// JS code run with this method can assume that their top-level
|
|
|
|
// declarations won't affect the global scope.
|
|
|
|
v8::MaybeLocal<v8::Value> CompileAndCall(
|
|
|
|
v8::Local<v8::Context> context,
|
|
|
|
const char* id,
|
2025-06-02 17:30:15 -04:00
|
|
|
v8::LocalVector<v8::String>* parameters,
|
|
|
|
v8::LocalVector<v8::Value>* arguments);
|
2019-10-23 09:26:32 -07:00
|
|
|
|
2025-01-23 08:55:28 -06:00
|
|
|
// Wrapper for node::CreateEnvironment that logs failure
|
|
|
|
node::Environment* CreateEnvironment(v8::Isolate* isolate,
|
|
|
|
node::IsolateData* isolate_data,
|
|
|
|
v8::Local<v8::Context> context,
|
|
|
|
const std::vector<std::string>& args,
|
|
|
|
const std::vector<std::string>& exec_args,
|
|
|
|
node::EnvironmentFlags::Flags env_flags,
|
|
|
|
std::string_view process_type = "");
|
|
|
|
|
2025-05-07 19:21:39 -06:00
|
|
|
// A scope that temporarily changes the microtask policy to explicit. Use this
|
|
|
|
// anywhere that can trigger Node.js or uv_run().
|
|
|
|
//
|
|
|
|
// Node.js expects `kExplicit` microtasks policy and will run microtasks
|
|
|
|
// checkpoints after every call into JavaScript. Since we use a different
|
|
|
|
// policy in the renderer, this scope temporarily changes the policy to
|
|
|
|
// `kExplicit` while the scope is active, then restores the original policy
|
|
|
|
// when it's destroyed.
|
|
|
|
class ExplicitMicrotasksScope {
|
|
|
|
public:
|
|
|
|
explicit ExplicitMicrotasksScope(v8::MicrotaskQueue* queue);
|
|
|
|
~ExplicitMicrotasksScope();
|
|
|
|
|
|
|
|
ExplicitMicrotasksScope(const ExplicitMicrotasksScope&) = delete;
|
|
|
|
ExplicitMicrotasksScope& operator=(const ExplicitMicrotasksScope&) = delete;
|
|
|
|
|
|
|
|
private:
|
|
|
|
base::raw_ptr<v8::MicrotaskQueue> microtask_queue_;
|
|
|
|
v8::MicrotasksPolicy original_policy_;
|
|
|
|
};
|
|
|
|
|
2025-04-25 08:00:09 -05:00
|
|
|
} // namespace electron::util
|
|
|
|
|
|
|
|
namespace electron::Buffer {
|
|
|
|
|
2024-10-10 08:34:55 -05:00
|
|
|
// Convenience function to view a Node buffer's data as a base::span().
|
|
|
|
// Analogous to base::as_byte_span()
|
|
|
|
[[nodiscard]] base::span<uint8_t> as_byte_span(
|
|
|
|
v8::Local<v8::Value> node_buffer);
|
|
|
|
|
2025-04-25 08:00:09 -05:00
|
|
|
// span-friendly version of node::Buffer::Copy()
|
|
|
|
[[nodiscard]] v8::MaybeLocal<v8::Object> Copy(v8::Isolate* isolate,
|
|
|
|
base::span<const char> data);
|
|
|
|
|
|
|
|
// span-friendly version of node::Buffer::Copy()
|
|
|
|
[[nodiscard]] v8::MaybeLocal<v8::Object> Copy(v8::Isolate* isolate,
|
|
|
|
base::span<const uint8_t> data);
|
|
|
|
|
|
|
|
} // namespace electron::Buffer
|
2019-10-23 09:26:32 -07:00
|
|
|
|
2021-11-22 08:34:31 +01:00
|
|
|
#endif // ELECTRON_SHELL_COMMON_NODE_UTIL_H_
|