|
1 | 1 | #include "node_dotenv.h" |
2 | 2 | #include "env-inl.h" |
3 | 3 | #include "node_file.h" |
| 4 | +#include "util.h" |
4 | 5 | #include "uv.h" |
| 6 | +#include "v8.h" |
5 | 7 |
|
6 | 8 | namespace node { |
7 | 9 |
|
| 10 | +using v8::Context; |
| 11 | +using v8::FunctionCallbackInfo; |
| 12 | +using v8::HandleScope; |
| 13 | +using v8::Isolate; |
| 14 | +using v8::Local; |
8 | 15 | using v8::NewStringType; |
| 16 | +using v8::Object; |
| 17 | +using v8::ObjectTemplate; |
9 | 18 | using v8::String; |
| 19 | +using v8::Value; |
10 | 20 |
|
11 | 21 | std::vector<std::string> Dotenv::GetPathFromArgs( |
12 | 22 | const std::vector<std::string>& args) { |
@@ -167,4 +177,79 @@ void Dotenv::ParseLine(const std::string_view line) { |
167 | 177 | store_.insert_or_assign(std::string(key), value); |
168 | 178 | } |
169 | 179 |
|
| 180 | +namespace dotenv { |
| 181 | + |
| 182 | +void BindingData::MemoryInfo(MemoryTracker* tracker) const { |
| 183 | + // Do nothing |
| 184 | +} |
| 185 | + |
| 186 | +BindingData::BindingData(Realm* realm, |
| 187 | + v8::Local<v8::Object> object, |
| 188 | + InternalFieldInfo* info) |
| 189 | + : SnapshotableObject(realm, object, type_int) {} |
| 190 | + |
| 191 | +bool BindingData::PrepareForSerialization(v8::Local<v8::Context> context, |
| 192 | + v8::SnapshotCreator* creator) { |
| 193 | + return true; |
| 194 | +} |
| 195 | + |
| 196 | +InternalFieldInfoBase* BindingData::Serialize(int index) { |
| 197 | + DCHECK_IS_SNAPSHOT_SLOT(index); |
| 198 | + InternalFieldInfo* info = |
| 199 | + InternalFieldInfoBase::New<InternalFieldInfo>(type()); |
| 200 | + return info; |
| 201 | +} |
| 202 | + |
| 203 | +void BindingData::Deserialize(v8::Local<v8::Context> context, |
| 204 | + v8::Local<v8::Object> holder, |
| 205 | + int index, |
| 206 | + InternalFieldInfoBase* info) { |
| 207 | + DCHECK_IS_SNAPSHOT_SLOT(index); |
| 208 | + HandleScope scope(context->GetIsolate()); |
| 209 | + Realm* realm = Realm::GetCurrent(context); |
| 210 | + BindingData* binding = realm->AddBindingData<BindingData>(holder); |
| 211 | + CHECK_NOT_NULL(binding); |
| 212 | +} |
| 213 | + |
| 214 | +void BindingData::Load(const FunctionCallbackInfo<Value>& args) { |
| 215 | + Environment* env = Environment::GetCurrent(args); |
| 216 | + std::string path = ".env"; |
| 217 | + if (args.Length() == 1) { |
| 218 | + CHECK(args[0]->IsString()); |
| 219 | + Utf8Value path_value(args.GetIsolate(), args[0]); |
| 220 | + path = path_value.ToString(); |
| 221 | + } |
| 222 | + |
| 223 | + Dotenv dotenv{}; |
| 224 | + dotenv.ParsePath(path); |
| 225 | + dotenv.SetEnvironment(env); |
| 226 | +} |
| 227 | + |
| 228 | +void BindingData::CreatePerIsolateProperties(IsolateData* isolate_data, |
| 229 | + Local<ObjectTemplate> target) { |
| 230 | + Isolate* isolate = isolate_data->isolate(); |
| 231 | + SetMethod(isolate, target, "load", Load); |
| 232 | +} |
| 233 | + |
| 234 | +void BindingData::CreatePerContextProperties(Local<Object> target, |
| 235 | + Local<Value> unused, |
| 236 | + Local<Context> context, |
| 237 | + void* priv) { |
| 238 | + Realm* realm = Realm::GetCurrent(context); |
| 239 | + realm->AddBindingData<BindingData>(target); |
| 240 | +} |
| 241 | + |
| 242 | +void BindingData::RegisterExternalReferences( |
| 243 | + ExternalReferenceRegistry* registry) { |
| 244 | + registry->Register(Load); |
| 245 | +} |
| 246 | + |
| 247 | +} // namespace dotenv |
170 | 248 | } // namespace node |
| 249 | + |
| 250 | +NODE_BINDING_CONTEXT_AWARE_INTERNAL( |
| 251 | + dotenv, node::dotenv::BindingData::CreatePerContextProperties) |
| 252 | +NODE_BINDING_PER_ISOLATE_INIT( |
| 253 | + dotenv, node::dotenv::BindingData::CreatePerIsolateProperties) |
| 254 | +NODE_BINDING_EXTERNAL_REFERENCE( |
| 255 | + dotenv, node::dotenv::BindingData::RegisterExternalReferences) |
0 commit comments