|
| 1 | +// Flags: --expose-internals --no-warnings --allow-natives-syntax |
| 2 | +'use strict'; |
| 3 | + |
| 4 | +const common = require('../common'); |
| 5 | +const assert = require('assert'); |
| 6 | +const { internalBinding } = require('internal/test/binding'); |
| 7 | + |
| 8 | +// Get direct access to the buffer validation methods |
| 9 | +const buffer = require('buffer'); |
| 10 | + |
| 11 | +// Create test buffers |
| 12 | +const utf8Buffer = Buffer.from('Hello, 世界!'); // Valid UTF-8 with actual Unicode characters |
| 13 | +const asciiBuffer = Buffer.from('Hello, World!'); // Valid ASCII |
| 14 | +const nonUtf8Buffer = Buffer.from([0xFF, 0xFF, 0xFF]); // Invalid UTF-8 |
| 15 | +const nonAsciiBuffer = Buffer.from([0x80, 0x90, 0xA0]); // Invalid ASCII |
| 16 | + |
| 17 | +// Test basic functionality for isUtf8 |
| 18 | +assert.strictEqual(buffer.isUtf8(utf8Buffer), true); |
| 19 | +assert.strictEqual(buffer.isUtf8(nonUtf8Buffer), false); |
| 20 | + |
| 21 | +// Test basic functionality for isAscii |
| 22 | +assert.strictEqual(buffer.isAscii(asciiBuffer), true); |
| 23 | +assert.strictEqual(buffer.isAscii(nonAsciiBuffer), false); |
| 24 | + |
| 25 | +// Test detached buffers |
| 26 | +const detachedBuffer = new ArrayBuffer(10); |
| 27 | +try { |
| 28 | + detachedBuffer.detach(); |
| 29 | +} catch (e) { |
| 30 | + console.log('Skipping detached buffer tests - detach not supported'); |
| 31 | +} |
| 32 | + |
| 33 | +if (detachedBuffer.detached) { |
| 34 | + const typedArray = new Uint8Array(detachedBuffer); |
| 35 | + |
| 36 | + assert.throws(() => { |
| 37 | + buffer.isUtf8(typedArray); |
| 38 | + }, { |
| 39 | + name: 'Error', |
| 40 | + code: 'ERR_INVALID_STATE' |
| 41 | + }); |
| 42 | + |
| 43 | + assert.throws(() => { |
| 44 | + buffer.isAscii(typedArray); |
| 45 | + }, { |
| 46 | + name: 'Error', |
| 47 | + code: 'ERR_INVALID_STATE' |
| 48 | + }); |
| 49 | +} |
| 50 | + |
| 51 | +// Test optimization and fast API paths |
| 52 | +function testFastPaths() { |
| 53 | + // Test both valid and invalid cases to ensure both paths are optimized |
| 54 | + buffer.isUtf8(utf8Buffer); |
| 55 | + buffer.isUtf8(nonUtf8Buffer); |
| 56 | + buffer.isAscii(asciiBuffer); |
| 57 | + buffer.isAscii(nonAsciiBuffer); |
| 58 | +} |
| 59 | + |
| 60 | +// Since we want to optimize the C++ methods, we need to prepare them |
| 61 | +// through their JS wrappers |
| 62 | +eval('%PrepareFunctionForOptimization(buffer.isUtf8)'); |
| 63 | +eval('%PrepareFunctionForOptimization(buffer.isAscii)'); |
| 64 | +testFastPaths(); |
| 65 | +eval('%OptimizeFunctionOnNextCall(buffer.isUtf8)'); |
| 66 | +eval('%OptimizeFunctionOnNextCall(buffer.isAscii)'); |
| 67 | +testFastPaths(); |
| 68 | + |
| 69 | +// Verify fast API calls were made if running in debug mode |
| 70 | +if (common.isDebug) { |
| 71 | + const { getV8FastApiCallCount } = internalBinding('debug'); |
| 72 | + assert.strictEqual(getV8FastApiCallCount('buffer.isUtf8'), 2); // Called twice in testFastPaths |
| 73 | + assert.strictEqual(getV8FastApiCallCount('buffer.isAscii'), 2); // Called twice in testFastPaths |
| 74 | +} |
0 commit comments