@@ -273,144 +273,16 @@ inline WriteWrap* StreamBase::CreateWriteWrap(
273
273
return new SimpleWriteWrap<AsyncWrap>(this , object);
274
274
}
275
275
276
- template <class Base >
277
- void StreamBase::AddMethods (Environment* env, Local<FunctionTemplate> t) {
278
- HandleScope scope (env->isolate ());
279
-
280
- enum PropertyAttribute attributes =
281
- static_cast <PropertyAttribute>(
282
- v8::ReadOnly | v8::DontDelete | v8::DontEnum);
283
-
284
- Local<Signature> signature = Signature::New (env->isolate (), t);
285
-
286
- Local<FunctionTemplate> get_fd_templ =
287
- env->NewFunctionTemplate (GetFD<Base>,
288
- signature,
289
- v8::ConstructorBehavior::kThrow ,
290
- v8::SideEffectType::kHasNoSideEffect );
291
-
292
- Local<FunctionTemplate> get_external_templ =
293
- env->NewFunctionTemplate (GetExternal<Base>,
294
- signature,
295
- v8::ConstructorBehavior::kThrow ,
296
- v8::SideEffectType::kHasNoSideEffect );
297
-
298
- Local<FunctionTemplate> get_bytes_read_templ =
299
- env->NewFunctionTemplate (GetBytesRead<Base>,
300
- signature,
301
- v8::ConstructorBehavior::kThrow ,
302
- v8::SideEffectType::kHasNoSideEffect );
303
-
304
- Local<FunctionTemplate> get_bytes_written_templ =
305
- env->NewFunctionTemplate (GetBytesWritten<Base>,
306
- signature,
307
- v8::ConstructorBehavior::kThrow ,
308
- v8::SideEffectType::kHasNoSideEffect );
309
-
310
- t->PrototypeTemplate ()->SetAccessorProperty (env->fd_string (),
311
- get_fd_templ,
312
- Local<FunctionTemplate>(),
313
- attributes);
314
-
315
- t->PrototypeTemplate ()->SetAccessorProperty (env->external_stream_string (),
316
- get_external_templ,
317
- Local<FunctionTemplate>(),
318
- attributes);
319
-
320
- t->PrototypeTemplate ()->SetAccessorProperty (env->bytes_read_string (),
321
- get_bytes_read_templ,
322
- Local<FunctionTemplate>(),
323
- attributes);
324
-
325
- t->PrototypeTemplate ()->SetAccessorProperty (env->bytes_written_string (),
326
- get_bytes_written_templ,
327
- Local<FunctionTemplate>(),
328
- attributes);
329
-
330
- env->SetProtoMethod (t, " readStart" , JSMethod<Base, &StreamBase::ReadStartJS>);
331
- env->SetProtoMethod (t, " readStop" , JSMethod<Base, &StreamBase::ReadStopJS>);
332
- env->SetProtoMethod (t, " shutdown" , JSMethod<Base, &StreamBase::Shutdown>);
333
- env->SetProtoMethod (t, " writev" , JSMethod<Base, &StreamBase::Writev>);
334
- env->SetProtoMethod (t,
335
- " writeBuffer" ,
336
- JSMethod<Base, &StreamBase::WriteBuffer>);
337
- env->SetProtoMethod (t,
338
- " writeAsciiString" ,
339
- JSMethod<Base, &StreamBase::WriteString<ASCII> >);
340
- env->SetProtoMethod (t,
341
- " writeUtf8String" ,
342
- JSMethod<Base, &StreamBase::WriteString<UTF8> >);
343
- env->SetProtoMethod (t,
344
- " writeUcs2String" ,
345
- JSMethod<Base, &StreamBase::WriteString<UCS2> >);
346
- env->SetProtoMethod (t,
347
- " writeLatin1String" ,
348
- JSMethod<Base, &StreamBase::WriteString<LATIN1> >);
276
+ inline void StreamBase::AttachToObject (v8::Local<v8::Object> obj) {
277
+ obj->SetAlignedPointerInInternalField (kStreamBaseField , this );
349
278
}
350
279
280
+ inline StreamBase* StreamBase::FromObject (v8::Local<v8::Object> obj) {
281
+ if (obj->GetAlignedPointerFromInternalField (0 ) == nullptr )
282
+ return nullptr ;
351
283
352
- template <class Base >
353
- void StreamBase::GetFD (const FunctionCallbackInfo<Value>& args) {
354
- // Mimic implementation of StreamBase::GetFD() and UDPWrap::GetFD().
355
- Base* handle;
356
- ASSIGN_OR_RETURN_UNWRAP (&handle,
357
- args.This (),
358
- args.GetReturnValue ().Set (UV_EINVAL));
359
-
360
- StreamBase* wrap = static_cast <StreamBase*>(handle);
361
- if (!wrap->IsAlive ())
362
- return args.GetReturnValue ().Set (UV_EINVAL);
363
-
364
- args.GetReturnValue ().Set (wrap->GetFD ());
365
- }
366
-
367
- template <class Base >
368
- void StreamBase::GetBytesRead (const FunctionCallbackInfo<Value>& args) {
369
- Base* handle;
370
- ASSIGN_OR_RETURN_UNWRAP (&handle,
371
- args.This (),
372
- args.GetReturnValue ().Set (0 ));
373
-
374
- StreamBase* wrap = static_cast <StreamBase*>(handle);
375
- // uint64_t -> double. 53bits is enough for all real cases.
376
- args.GetReturnValue ().Set (static_cast <double >(wrap->bytes_read_ ));
377
- }
378
-
379
- template <class Base >
380
- void StreamBase::GetBytesWritten (const FunctionCallbackInfo<Value>& args) {
381
- Base* handle;
382
- ASSIGN_OR_RETURN_UNWRAP (&handle,
383
- args.This (),
384
- args.GetReturnValue ().Set (0 ));
385
-
386
- StreamBase* wrap = static_cast <StreamBase*>(handle);
387
- // uint64_t -> double. 53bits is enough for all real cases.
388
- args.GetReturnValue ().Set (static_cast <double >(wrap->bytes_written_ ));
389
- }
390
-
391
- template <class Base >
392
- void StreamBase::GetExternal (const FunctionCallbackInfo<Value>& args) {
393
- Base* handle;
394
- ASSIGN_OR_RETURN_UNWRAP (&handle, args.This ());
395
-
396
- StreamBase* wrap = static_cast <StreamBase*>(handle);
397
- Local<External> ext = External::New (args.GetIsolate (), wrap);
398
- args.GetReturnValue ().Set (ext);
399
- }
400
-
401
-
402
- template <class Base ,
403
- int (StreamBase::*Method)(const FunctionCallbackInfo<Value>& args)>
404
- void StreamBase::JSMethod(const FunctionCallbackInfo<Value>& args) {
405
- Base* handle;
406
- ASSIGN_OR_RETURN_UNWRAP (&handle, args.Holder ());
407
-
408
- StreamBase* wrap = static_cast <StreamBase*>(handle);
409
- if (!wrap->IsAlive ())
410
- return args.GetReturnValue ().Set (UV_EINVAL);
411
-
412
- AsyncHooks::DefaultTriggerAsyncIdScope trigger_scope (handle);
413
- args.GetReturnValue ().Set ((wrap->*Method)(args));
284
+ return static_cast <StreamBase*>(
285
+ obj->GetAlignedPointerFromInternalField (kStreamBaseField ));
414
286
}
415
287
416
288
0 commit comments