|
1 |
| -/******************************************************************************* |
| 1 | +/******************************************************************************* |
2 | 2 | * Experimental prototype for demonstrating VM agnostic and ABI stable API
|
3 | 3 | * for native modules to use instead of using Nan and V8 APIs directly.
|
4 | 4 | *
|
@@ -517,7 +517,7 @@ namespace v8impl {
|
517 | 517 | napi_ok : napi_set_last_error(napi_pending_exception))
|
518 | 518 |
|
519 | 519 | // Static last error returned from napi_get_last_error_info
|
520 |
| -napi_extended_error_info static_last_error; |
| 520 | +napi_extended_error_info static_last_error = {}; |
521 | 521 |
|
522 | 522 | // Warning: Keep in-sync with napi_status enum
|
523 | 523 | const char* error_messages[] = {
|
@@ -1881,3 +1881,194 @@ napi_status napi_buffer_length(napi_env e, napi_value v, size_t* result) {
|
1881 | 1881 | *result = node::Buffer::Length(v8impl::V8LocalValueFromJsValue(v));
|
1882 | 1882 | return GET_RETURN_STATUS();
|
1883 | 1883 | }
|
| 1884 | + |
| 1885 | +napi_status napi_is_arraybuffer(napi_env e, napi_value value, bool* result) { |
| 1886 | + NAPI_PREAMBLE(e); |
| 1887 | + CHECK_ARG(result); |
| 1888 | + |
| 1889 | + v8::Local<v8::Value> v8value = v8impl::V8LocalValueFromJsValue(value); |
| 1890 | + *result = v8value->IsArrayBuffer(); |
| 1891 | + |
| 1892 | + return GET_RETURN_STATUS(); |
| 1893 | +} |
| 1894 | + |
| 1895 | +napi_status napi_create_arraybuffer(napi_env e, |
| 1896 | + size_t byte_length, |
| 1897 | + void** data, |
| 1898 | + napi_value* result) { |
| 1899 | + NAPI_PREAMBLE(e); |
| 1900 | + CHECK_ARG(result); |
| 1901 | + |
| 1902 | + v8::Isolate *isolate = v8impl::V8IsolateFromJsEnv(e); |
| 1903 | + v8::Local<v8::ArrayBuffer> buffer = v8::ArrayBuffer::New(isolate, byte_length); |
| 1904 | + |
| 1905 | + // Optionally return a pointer to the buffer's data, to avoid another call to retreive it. |
| 1906 | + if (data != nullptr) { |
| 1907 | + *data = buffer->GetContents().Data(); |
| 1908 | + } |
| 1909 | + |
| 1910 | + *result = v8impl::JsValueFromV8LocalValue(buffer); |
| 1911 | + return GET_RETURN_STATUS(); |
| 1912 | +} |
| 1913 | + |
| 1914 | +napi_status napi_create_external_arraybuffer(napi_env e, |
| 1915 | + void* external_data, |
| 1916 | + size_t byte_length, |
| 1917 | + napi_value* result) { |
| 1918 | + NAPI_PREAMBLE(e); |
| 1919 | + CHECK_ARG(result); |
| 1920 | + |
| 1921 | + v8::Isolate *isolate = v8impl::V8IsolateFromJsEnv(e); |
| 1922 | + v8::Local<v8::ArrayBuffer> buffer = |
| 1923 | + v8::ArrayBuffer::New(isolate, external_data, byte_length); |
| 1924 | + |
| 1925 | + *result = v8impl::JsValueFromV8LocalValue(buffer); |
| 1926 | + return GET_RETURN_STATUS(); |
| 1927 | +} |
| 1928 | + |
| 1929 | +napi_status napi_get_arraybuffer_info(napi_env e, |
| 1930 | + napi_value arraybuffer, |
| 1931 | + void** data, |
| 1932 | + size_t* byte_length) { |
| 1933 | + NAPI_PREAMBLE(e); |
| 1934 | + |
| 1935 | + v8::Local<v8::Value> value = v8impl::V8LocalValueFromJsValue(arraybuffer); |
| 1936 | + RETURN_STATUS_IF_FALSE(value->IsArrayBuffer(), napi_invalid_arg); |
| 1937 | + |
| 1938 | + v8::ArrayBuffer::Contents contents = value.As<v8::ArrayBuffer>()->GetContents(); |
| 1939 | + |
| 1940 | + if (data != nullptr) { |
| 1941 | + *data = contents.Data(); |
| 1942 | + } |
| 1943 | + |
| 1944 | + if (byte_length != nullptr) { |
| 1945 | + *byte_length = contents.ByteLength(); |
| 1946 | + } |
| 1947 | + |
| 1948 | + return GET_RETURN_STATUS(); |
| 1949 | +} |
| 1950 | + |
| 1951 | +napi_status napi_is_typedarray(napi_env e, napi_value value, bool* result) { |
| 1952 | + NAPI_PREAMBLE(e); |
| 1953 | + CHECK_ARG(result); |
| 1954 | + |
| 1955 | + v8::Local<v8::Value> v8value = v8impl::V8LocalValueFromJsValue(value); |
| 1956 | + *result = v8value->IsTypedArray(); |
| 1957 | + |
| 1958 | + return GET_RETURN_STATUS(); |
| 1959 | +} |
| 1960 | + |
| 1961 | +napi_status napi_create_typedarray(napi_env e, |
| 1962 | + napi_typedarray_type type, |
| 1963 | + size_t length, |
| 1964 | + napi_value arraybuffer, |
| 1965 | + size_t byte_offset, |
| 1966 | + napi_value* result) { |
| 1967 | + NAPI_PREAMBLE(e); |
| 1968 | + CHECK_ARG(result); |
| 1969 | + |
| 1970 | + v8::Local<v8::Value> value = v8impl::V8LocalValueFromJsValue(arraybuffer); |
| 1971 | + RETURN_STATUS_IF_FALSE(value->IsArrayBuffer(), napi_invalid_arg); |
| 1972 | + |
| 1973 | + v8::Local<v8::ArrayBuffer> buffer = value.As<v8::ArrayBuffer>(); |
| 1974 | + v8::Local<v8::TypedArray> typedArray; |
| 1975 | + |
| 1976 | + switch (type) { |
| 1977 | + case napi_int8: |
| 1978 | + typedArray = v8::Int8Array::New(buffer, byte_offset, length); |
| 1979 | + break; |
| 1980 | + case napi_uint8: |
| 1981 | + typedArray = v8::Uint8Array::New(buffer, byte_offset, length); |
| 1982 | + break; |
| 1983 | + case napi_uint8_clamped: |
| 1984 | + typedArray = v8::Uint8ClampedArray::New(buffer, byte_offset, length); |
| 1985 | + break; |
| 1986 | + case napi_int16: |
| 1987 | + typedArray = v8::Int16Array::New(buffer, byte_offset, length); |
| 1988 | + break; |
| 1989 | + case napi_uint16: |
| 1990 | + typedArray = v8::Uint16Array::New(buffer, byte_offset, length); |
| 1991 | + break; |
| 1992 | + case napi_int32: |
| 1993 | + typedArray = v8::Int32Array::New(buffer, byte_offset, length); |
| 1994 | + break; |
| 1995 | + case napi_uint32: |
| 1996 | + typedArray = v8::Uint32Array::New(buffer, byte_offset, length); |
| 1997 | + break; |
| 1998 | + case napi_float32: |
| 1999 | + typedArray = v8::Float32Array::New(buffer, byte_offset, length); |
| 2000 | + break; |
| 2001 | + case napi_float64: |
| 2002 | + typedArray = v8::Float64Array::New(buffer, byte_offset, length); |
| 2003 | + break; |
| 2004 | + default: |
| 2005 | + return napi_set_last_error(napi_invalid_arg); |
| 2006 | + } |
| 2007 | + |
| 2008 | + *result = v8impl::JsValueFromV8LocalValue(typedArray); |
| 2009 | + return GET_RETURN_STATUS(); |
| 2010 | +} |
| 2011 | + |
| 2012 | +napi_status napi_get_typedarray_info(napi_env e, |
| 2013 | + napi_value typedarray, |
| 2014 | + napi_typedarray_type* type, |
| 2015 | + size_t* length, |
| 2016 | + void** data, |
| 2017 | + napi_value* arraybuffer, |
| 2018 | + size_t* byte_offset) { |
| 2019 | + NAPI_PREAMBLE(e); |
| 2020 | + |
| 2021 | + v8::Local<v8::Value> value = v8impl::V8LocalValueFromJsValue(typedarray); |
| 2022 | + RETURN_STATUS_IF_FALSE(value->IsTypedArray(), napi_invalid_arg); |
| 2023 | + |
| 2024 | + v8::Local<v8::TypedArray> array = value.As<v8::TypedArray>(); |
| 2025 | + |
| 2026 | + if (type != nullptr) { |
| 2027 | + if (value->IsInt8Array()) { |
| 2028 | + *type = napi_int8; |
| 2029 | + } |
| 2030 | + else if (value->IsUint8Array()) { |
| 2031 | + *type = napi_uint8; |
| 2032 | + } |
| 2033 | + else if (value->IsUint8ClampedArray()) { |
| 2034 | + *type = napi_uint8_clamped; |
| 2035 | + } |
| 2036 | + else if (value->IsInt16Array()) { |
| 2037 | + *type = napi_int16; |
| 2038 | + } |
| 2039 | + else if (value->IsUint16Array()) { |
| 2040 | + *type = napi_uint16; |
| 2041 | + } |
| 2042 | + else if (value->IsInt32Array()) { |
| 2043 | + *type = napi_int32; |
| 2044 | + } |
| 2045 | + else if (value->IsUint32Array()) { |
| 2046 | + *type = napi_uint32; |
| 2047 | + } |
| 2048 | + else if (value->IsFloat32Array()) { |
| 2049 | + *type = napi_float32; |
| 2050 | + } |
| 2051 | + else if (value->IsFloat64Array()) { |
| 2052 | + *type = napi_float64; |
| 2053 | + } |
| 2054 | + } |
| 2055 | + |
| 2056 | + if (length != nullptr) { |
| 2057 | + *length = array->Length(); |
| 2058 | + } |
| 2059 | + |
| 2060 | + v8::Local<v8::ArrayBuffer> buffer = array->Buffer(); |
| 2061 | + if (data != nullptr) { |
| 2062 | + *data = static_cast<uint8_t*>(buffer->GetContents().Data()) + array->ByteOffset(); |
| 2063 | + } |
| 2064 | + |
| 2065 | + if (arraybuffer != nullptr) { |
| 2066 | + *arraybuffer = v8impl::JsValueFromV8LocalValue(buffer); |
| 2067 | + } |
| 2068 | + |
| 2069 | + if (byte_offset != nullptr) { |
| 2070 | + *byte_offset = array->ByteOffset(); |
| 2071 | + } |
| 2072 | + |
| 2073 | + return GET_RETURN_STATUS(); |
| 2074 | +} |
0 commit comments