Skip to content

Commit 3efb492

Browse files
committed
scratch()
1 parent a6f69eb commit 3efb492

File tree

4 files changed

+76
-4
lines changed

4 files changed

+76
-4
lines changed

_.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const util = require('util');
2+
const _ = require('crypto').scratch;
3+
4+
console.log('const scratch = ' + _);
5+
6+
function scratch(v, die) {
7+
console.log('===== scratch(', util.inspect(v), ',', !!die, ')');
8+
try {
9+
console.log('returns:', _(v, die));
10+
} catch (er) {
11+
console.log('throws:', er.message);
12+
}
13+
}
14+
15+
scratch(1.1);
16+
scratch();
17+
scratch('12');
18+
scratch(null);
19+
scratch(-3);
20+
scratch(12);
21+
scratch(function () { const hello=0; });
22+
scratch(true);

lib/crypto.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ const {
9090
setDefaultEncoding,
9191
setEngine,
9292
timingSafeEqual,
93-
toBuf
93+
toBuf,
94+
scratch,
9495
} = require('internal/crypto/util');
9596
const Certificate = require('internal/crypto/certificate');
9697

@@ -170,6 +171,7 @@ module.exports = exports = {
170171
scryptSync,
171172
setEngine,
172173
timingSafeEqual,
174+
scratch,
173175
getFips: !fipsMode ? getFipsDisabled :
174176
fipsForced ? getFipsForced : getFipsCrypto,
175177
setFips: !fipsMode ? setFipsDisabled :

lib/internal/crypto/util.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ const {
55
getCurves: _getCurves,
66
getHashes: _getHashes,
77
setEngine: _setEngine,
8-
timingSafeEqual: _timingSafeEqual
8+
timingSafeEqual: _timingSafeEqual,
9+
scratch
910
} = internalBinding('crypto');
1011

1112
const {
@@ -120,5 +121,6 @@ module.exports = {
120121
setDefaultEncoding,
121122
setEngine,
122123
timingSafeEqual,
123-
toBuf
124+
toBuf,
125+
scratch,
124126
};

src/node_crypto.cc

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
1919
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
2020
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21-
21+
#undef V8_IMMINENT_DEPRECATION_WARNINGS
22+
#undef V8_DEPRECATION_WARNINGS
2223
#include "node.h"
2324
#include "node_buffer.h"
2425
#include "node_errors.h"
@@ -401,6 +402,50 @@ void SecureContext::New(const FunctionCallbackInfo<Value>& args) {
401402
new SecureContext(env, args.This());
402403
}
403404

405+
const char* b(bool _) { return _ ? "true" : "false"; }
406+
407+
void Scratch(const FunctionCallbackInfo<Value>& args) {
408+
Environment* env = Environment::GetCurrent(args);
409+
Isolate* i = env->isolate();
410+
Local<Context> c = env->context();
411+
// or Local<Context> c = i->GetCurrentContext();
412+
413+
Local<Value> lv = args[0];
414+
bool die = args[1].As<Boolean>()->Value();
415+
416+
if (die)
417+
env->ThrowError("as you wish");
418+
419+
printf("IsUndefined()? %s\n", b(lv->IsUndefined()));
420+
printf("IsNumber()? %s\n", b(lv->IsNumber()));
421+
printf("IsInt32()? %s\n", b(lv->IsInt32()));
422+
printf("IsUint32()? %s\n", b(lv->IsUint32()));
423+
printf("IsBoolean()? %s\n", b(lv->IsBoolean()));
424+
printf("IsFunction()? %s\n", b(lv->IsFunction()));
425+
printf("IsString()? %s\n", b(lv->IsString()));
426+
427+
// XXX how can I make ml.IsEmpty() be true for any ToT() conversions?
428+
429+
// ToT(context) returns a maybe, ToT(isolate) returns a local, one or the
430+
// other will be deprecated, apparently depends on whether it can "fail". What
431+
// causes "failure"?
432+
{ Local<Int32> l = lv.As<Int32>(); int32_t v = l->Value(); printf("as int32_t %d\n", v); }
433+
{ MaybeLocal<Int32> ml = lv->ToInt32(c); Local<Int32> l = ml.ToLocalChecked(); int32_t v = l->Value(); printf("to int32_t %d\n", v); }
434+
435+
{ Local<Uint32> l = lv.As<Uint32>(); uint32_t v = l->Value(); printf("as uint32_t %u\n", v); }
436+
{ MaybeLocal<Uint32> ml = lv->ToUint32(c); Local<Uint32> l = ml.ToLocalChecked(); uint32_t v = l->Value(); printf("to uint32_t %u\n", v); }
437+
438+
{ Local<Boolean> l = lv.As<Boolean>(); bool v = l->Value(); printf("as bool %s\n", b(v)); }
439+
// ToBoolean "cannot fail" - wtf?
440+
{ Local<Boolean> l = lv->ToBoolean(i); bool v = l->Value(); printf("to bool %s\n", b(v)); }
441+
442+
{ const node::Utf8Value s(env->isolate(), args[0]); printf("as char* \"%s\"\n", *s); }
443+
{ MaybeLocal<String> ml = lv->ToString(i); Local<String> l = ml.ToLocalChecked();
444+
char v[123]; l->WriteUtf8(i, v, sizeof(v)); printf("to char* \"%s\"\n", v); }
445+
446+
int len = args.Length();
447+
args.GetReturnValue().Set(len);
448+
}
404449

405450
void SecureContext::Init(const FunctionCallbackInfo<Value>& args) {
406451
SecureContext* sc;
@@ -5812,6 +5857,7 @@ void Initialize(Local<Object> target,
58125857
NODE_DEFINE_CONSTANT(target, PK_FORMAT_DER);
58135858
NODE_DEFINE_CONSTANT(target, PK_FORMAT_PEM);
58145859
env->SetMethod(target, "randomBytes", RandomBytes);
5860+
env->SetMethod(target, "scratch", Scratch);
58155861
env->SetMethodNoSideEffect(target, "timingSafeEqual", TimingSafeEqual);
58165862
env->SetMethodNoSideEffect(target, "getSSLCiphers", GetSSLCiphers);
58175863
env->SetMethodNoSideEffect(target, "getCiphers", GetCiphers);

0 commit comments

Comments
 (0)