From: Olivia Ryan <olivia.r.dev@gmail.com> --- dlls/windows.web/Makefile.in | 1 + dlls/windows.web/classes.idl | 1 + dlls/windows.web/json_array.c | 319 ++++++++++++++++++++++++++++ dlls/windows.web/json_object.c | 127 +++++++++-- dlls/windows.web/json_value.c | 374 ++++++++++++++++++++++++++++----- dlls/windows.web/main.c | 2 + dlls/windows.web/private.h | 5 + dlls/windows.web/tests/web.c | 50 ++++- 8 files changed, 802 insertions(+), 77 deletions(-) create mode 100644 dlls/windows.web/json_array.c diff --git a/dlls/windows.web/Makefile.in b/dlls/windows.web/Makefile.in index ebcf7bbc2f3..aaaacd6b298 100644 --- a/dlls/windows.web/Makefile.in +++ b/dlls/windows.web/Makefile.in @@ -3,6 +3,7 @@ IMPORTS = combase SOURCES = \ classes.idl \ + json_array.c \ json_object.c \ json_value.c \ main.c diff --git a/dlls/windows.web/classes.idl b/dlls/windows.web/classes.idl index 37537cfeb26..266f560af01 100644 --- a/dlls/windows.web/classes.idl +++ b/dlls/windows.web/classes.idl @@ -24,6 +24,7 @@ import "windows.data.json.idl"; namespace Windows.Data.Json { + runtimeclass JsonArray; runtimeclass JsonObject; runtimeclass JsonValue; } diff --git a/dlls/windows.web/json_array.c b/dlls/windows.web/json_array.c new file mode 100644 index 00000000000..943c0166d61 --- /dev/null +++ b/dlls/windows.web/json_array.c @@ -0,0 +1,319 @@ +/* WinRT Windows.Data.Json.JsonArray Implementation + * + * Copyright (C) 2026 Olivia Ryan + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA + */ + +#include "private.h" +#include "wine/debug.h" + +WINE_DEFAULT_DEBUG_CHANNEL(web); + +struct json_array +{ + IJsonArray IJsonArray_iface; + LONG ref; + IJsonValue **elements; + ULONG length; + ULONG size; +}; + +static inline struct json_array *impl_from_IJsonArray( IJsonArray *iface ) +{ + return CONTAINING_RECORD( iface, struct json_array, IJsonArray_iface ); +} + +HRESULT json_array_push( IJsonArray *iface, IJsonValue *value ) +{ + struct json_array *impl = impl_from_IJsonArray( iface ); + IJsonValue **new = impl->elements; + + TRACE( "iface %p, value %p.\n", iface, value ); + + if (impl->length == impl->size) + new = realloc(new, ++impl->size * sizeof(*new)); + + if (!new) + { + impl->size--; + return E_OUTOFMEMORY; + } + + impl->elements = new; + impl->elements[impl->length++] = value; + return S_OK; +} + +HRESULT json_array_pop( IJsonArray *iface, IJsonValue **value ) +{ + struct json_array *impl = impl_from_IJsonArray( iface ); + + TRACE( "iface %p, value %p.\n", iface, value ); + + if (impl->length == 0) return E_FAIL; + + if (value) *value = impl->elements[--impl->length]; + else IJsonValue_Release( impl->elements[--impl->length] ); + return S_OK; +} + +static HRESULT WINAPI json_array_statics_QueryInterface( IJsonArray *iface, REFIID iid, void **out ) +{ + struct json_array *impl = impl_from_IJsonArray( iface ); + + TRACE( "iface %p, iid %s, out %p.\n", iface, debugstr_guid( iid ), out ); + + if (IsEqualGUID( iid, &IID_IUnknown ) || + IsEqualGUID( iid, &IID_IInspectable ) || + IsEqualGUID( iid, &IID_IAgileObject ) || + IsEqualGUID( iid, &IID_IJsonArray )) + { + *out = &impl->IJsonArray_iface; + IInspectable_AddRef( *out ); + return S_OK; + } + + FIXME( "%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid( iid ) ); + *out = NULL; + return E_NOINTERFACE; +} + +static ULONG WINAPI json_array_statics_AddRef( IJsonArray *iface ) +{ + struct json_array *impl = impl_from_IJsonArray( iface ); + ULONG ref = InterlockedIncrement( &impl->ref ); + TRACE( "iface %p, ref %lu.\n", iface, ref ); + return ref; +} + +static ULONG WINAPI json_array_statics_Release( IJsonArray *iface ) +{ + struct json_array *impl = impl_from_IJsonArray( iface ); + ULONG ref = InterlockedDecrement( &impl->ref ); + + TRACE( "iface %p, ref %lu.\n", iface, ref ); + + if (!ref) + { + for (UINT32 i = 0; i < impl->length; i++) + IJsonValue_Release( impl->elements[i] ); + + if (impl->elements) free( impl->elements ); + free( impl ); + } + return ref; +} + +static HRESULT WINAPI json_array_statics_GetIids( IJsonArray *iface, ULONG *iid_count, IID **iids ) +{ + FIXME( "iface %p, iid_count %p, iids %p stub!\n", iface, iid_count, iids ); + return E_NOTIMPL; +} + +static HRESULT WINAPI json_array_statics_GetRuntimeClassName( IJsonArray *iface, HSTRING *class_name ) +{ + FIXME( "iface %p, class_name %p stub!\n", iface, class_name ); + return E_NOTIMPL; +} + +static HRESULT WINAPI json_array_statics_GetTrustLevel( IJsonArray *iface, TrustLevel *trust_level ) +{ + FIXME( "iface %p, trust_level %p stub!\n", iface, trust_level ); + return E_NOTIMPL; +} + +static HRESULT WINAPI json_array_statics_GetObjectAt( IJsonArray *iface, UINT32 index, IJsonObject **value ) +{ + struct json_array *impl = impl_from_IJsonArray( iface ); + + TRACE( "iface %p, index %u, value %p\n", iface, index, value ); + + if (!value) return E_POINTER; + if (index >= impl->length) return WEB_E_JSON_VALUE_NOT_FOUND; + + return IJsonValue_GetObject( impl->elements[index], value ); +} + +static HRESULT WINAPI json_array_statics_GetArrayAt( IJsonArray *iface, UINT32 index, IJsonArray **value ) +{ + struct json_array *impl = impl_from_IJsonArray( iface ); + + TRACE( "iface %p, index %u, value %p\n", iface, index, value ); + + if (!value) return E_POINTER; + if (index >= impl->length) return WEB_E_JSON_VALUE_NOT_FOUND; + + return IJsonValue_GetArray( impl->elements[index], value ); +} + +static HRESULT WINAPI json_array_statics_GetStringAt( IJsonArray *iface, UINT32 index, HSTRING *value ) +{ + struct json_array *impl = impl_from_IJsonArray( iface ); + + TRACE( "iface %p, index %u, value %p\n", iface, index, value ); + + if (!value) return E_POINTER; + if (index >= impl->length) return WEB_E_JSON_VALUE_NOT_FOUND; + + return IJsonValue_GetString( impl->elements[index], value ); +} + +static HRESULT WINAPI json_array_statics_GetNumberAt( IJsonArray *iface, UINT32 index, double *value ) +{ + struct json_array *impl = impl_from_IJsonArray( iface ); + + TRACE( "iface %p, index %u, value %p\n", iface, index, value ); + + if (!value) return E_POINTER; + if (index >= impl->length) return WEB_E_JSON_VALUE_NOT_FOUND; + + return IJsonValue_GetNumber( impl->elements[index], value ); +} + +static HRESULT WINAPI json_array_statics_GetBooleanAt( IJsonArray *iface, UINT32 index, boolean *value ) +{ + struct json_array *impl = impl_from_IJsonArray( iface ); + + TRACE( "iface %p, index %u, value %p\n", iface, index, value ); + + if (!value) return E_POINTER; + if (index >= impl->length) return WEB_E_JSON_VALUE_NOT_FOUND; + + return IJsonValue_GetBoolean( impl->elements[index], value ); +} + +static const struct IJsonArrayVtbl json_array_statics_vtbl = +{ + json_array_statics_QueryInterface, + json_array_statics_AddRef, + json_array_statics_Release, + /* IInspectable methods */ + json_array_statics_GetIids, + json_array_statics_GetRuntimeClassName, + json_array_statics_GetTrustLevel, + /* IJsonArray methods */ + json_array_statics_GetObjectAt, + json_array_statics_GetArrayAt, + json_array_statics_GetStringAt, + json_array_statics_GetNumberAt, + json_array_statics_GetBooleanAt, +}; + +struct json_array_statics +{ + IActivationFactory IActivationFactory_iface; + LONG ref; +}; + +static inline struct json_array_statics *impl_from_IActivationFactory( IActivationFactory *iface ) +{ + return CONTAINING_RECORD( iface, struct json_array_statics, IActivationFactory_iface ); +} + +static HRESULT WINAPI factory_QueryInterface( IActivationFactory *iface, REFIID iid, void **out ) +{ + struct json_array_statics *impl = impl_from_IActivationFactory( iface ); + + TRACE( "iface %p, iid %s, out %p.\n", iface, debugstr_guid(iid), out); + + if (IsEqualGUID( iid, &IID_IUnknown ) || + IsEqualGUID( iid, &IID_IInspectable ) || + IsEqualGUID( iid, &IID_IAgileObject ) || + IsEqualGUID( iid, &IID_IActivationFactory )) + { + *out = &impl->IActivationFactory_iface; + IInspectable_AddRef( *out ); + return S_OK; + } + + FIXME( "%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid( iid ) ); + *out = NULL; + return E_NOINTERFACE; +} + +static ULONG WINAPI factory_AddRef( IActivationFactory *iface ) +{ + struct json_array_statics *impl = impl_from_IActivationFactory( iface ); + ULONG ref = InterlockedIncrement( &impl->ref ); + TRACE( "iface %p, ref %lu.\n", iface, ref ); + return ref; +} + +static ULONG WINAPI factory_Release( IActivationFactory *iface ) +{ + struct json_array_statics *impl = impl_from_IActivationFactory( iface ); + ULONG ref = InterlockedDecrement( &impl->ref ); + TRACE( "iface %p, ref %lu.\n", iface, ref ); + return ref; +} + +static HRESULT WINAPI factory_GetIids( IActivationFactory *iface, ULONG *iid_count, IID **iids ) +{ + FIXME( "iface %p, iid_count %p, iids %p stub!\n", iface, iid_count, iids ); + return E_NOTIMPL; +} + +static HRESULT WINAPI factory_GetRuntimeClassName( IActivationFactory *iface, HSTRING *class_name ) +{ + FIXME( "iface %p, class_name %p stub!\n", iface, class_name ); + return E_NOTIMPL; +} + +static HRESULT WINAPI factory_GetTrustLevel( IActivationFactory *iface, TrustLevel *trust_level ) +{ + FIXME( "iface %p, trust_level %p stub!\n", iface, trust_level ); + return E_NOTIMPL; +} + +static HRESULT WINAPI factory_ActivateInstance( IActivationFactory *iface, IInspectable **instance ) +{ + struct json_array *impl; + + TRACE( "iface %p, instance %p.\n", iface, instance ); + + *instance = NULL; + if (!(impl = calloc( 1, sizeof(*impl) ))) return E_OUTOFMEMORY; + + impl->IJsonArray_iface.lpVtbl = &json_array_statics_vtbl; + impl->ref = 1; + impl->elements = NULL; + impl->length = 0; + impl->size = 0; + + *instance = (IInspectable*)&impl->IJsonArray_iface; + return S_OK; +} + +static const struct IActivationFactoryVtbl factory_vtbl = +{ + factory_QueryInterface, + factory_AddRef, + factory_Release, + /* IInspectable methods */ + factory_GetIids, + factory_GetRuntimeClassName, + factory_GetTrustLevel, + /* IActivationFactory methods */ + factory_ActivateInstance, +}; + +static struct json_array_statics json_array_statics = +{ + {&factory_vtbl}, + 1, +}; + +IActivationFactory *json_array_factory = &json_array_statics.IActivationFactory_iface; diff --git a/dlls/windows.web/json_object.c b/dlls/windows.web/json_object.c index 38c899288b7..43b46e45c3f 100644 --- a/dlls/windows.web/json_object.c +++ b/dlls/windows.web/json_object.c @@ -1,6 +1,7 @@ /* WinRT Windows.Data.Json.JsonObject Implementation * * Copyright (C) 2024 Mohamad Al-Jaf + * Copyright (C) 2026 Olivia Ryan * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -26,6 +27,7 @@ struct json_object { IJsonObject IJsonObject_iface; LONG ref; + IMap_HSTRING_IInspectable *members; }; static inline struct json_object *impl_from_IJsonObject( IJsonObject *iface ) @@ -69,7 +71,10 @@ static ULONG WINAPI json_object_statics_Release( IJsonObject *iface ) TRACE( "iface %p, ref %lu.\n", iface, ref ); - if (!ref) free( impl ); + if (!ref) { + IMap_HSTRING_IInspectable_Release( impl->members ); + free( impl ); + } return ref; } @@ -93,44 +98,115 @@ static HRESULT WINAPI json_object_statics_GetTrustLevel( IJsonObject *iface, Tru static HRESULT WINAPI json_object_statics_GetNamedValue( IJsonObject *iface, HSTRING name, IJsonValue **value ) { - FIXME( "iface %p, name %s, value %p stub!\n", iface, debugstr_hstring( name ), value ); - return E_NOTIMPL; + struct json_object *impl = impl_from_IJsonObject( iface ); + boolean exists; + HRESULT hr; + + TRACE( "iface %p, name %s, value %p.\n", iface, debugstr_hstring( name ), value ); + + if (!value) return E_POINTER; + + hr = IMap_HSTRING_IInspectable_HasKey( impl->members, name, &exists ); + if ( FAILED(hr) || !exists ) return WEB_E_JSON_VALUE_NOT_FOUND; + return IMap_HSTRING_IInspectable_Lookup( impl->members, name, (IInspectable**)value ); } static HRESULT WINAPI json_object_statics_SetNamedValue( IJsonObject *iface, HSTRING name, IJsonValue *value ) { - FIXME( "iface %p, name %s, value %p stub!\n", iface, debugstr_hstring( name ), value ); - return E_NOTIMPL; + boolean dummy; + struct json_object *impl = impl_from_IJsonObject( iface ); + TRACE( "iface %p, name %s, value %p.\n", iface, debugstr_hstring( name ), value ); + return IMap_HSTRING_IInspectable_Insert( impl->members, name, (IInspectable*)value, &dummy ); } static HRESULT WINAPI json_object_statics_GetNamedObject( IJsonObject *iface, HSTRING name, IJsonObject **value ) { - FIXME( "iface %p, name %s, value %p stub!\n", iface, debugstr_hstring( name ), value ); - return E_NOTIMPL; + IJsonValue *internal_value; + JsonValueType value_type; + HRESULT hr; + + TRACE( "iface %p, name %s, value %p.\n", iface, debugstr_hstring( name ), value ); + + if (FAILED(hr = IJsonObject_GetNamedValue( iface, name, &internal_value ))) + return hr; + + IJsonValue_Release( internal_value ); + IJsonValue_get_ValueType( internal_value, &value_type ); + if (value_type != JsonValueType_Object) return E_ILLEGAL_METHOD_CALL; + + return IJsonValue_GetObject( internal_value, value ); } static HRESULT WINAPI json_object_statics_GetNamedArray( IJsonObject *iface, HSTRING name, IJsonArray **value ) { - FIXME( "iface %p, name %s, value %p stub!\n", iface, debugstr_hstring( name ), value ); - return E_NOTIMPL; + IJsonValue *internal_value; + JsonValueType value_type; + HRESULT hr; + + TRACE( "iface %p, name %s, value %p.\n", iface, debugstr_hstring( name ), value ); + + if (FAILED(hr = IJsonObject_GetNamedValue( iface, name, &internal_value ))) + return hr; + + IJsonValue_Release( internal_value ); + IJsonValue_get_ValueType( internal_value, &value_type ); + if (value_type != JsonValueType_Array) return E_ILLEGAL_METHOD_CALL; + + return IJsonValue_GetArray( internal_value, value ); } static HRESULT WINAPI json_object_statics_GetNamedString( IJsonObject *iface, HSTRING name, HSTRING *value ) { - FIXME( "iface %p, name %s, value %p stub!\n", iface, debugstr_hstring( name ), value ); - return E_NOTIMPL; + IJsonValue *internal_value; + JsonValueType value_type; + HRESULT hr; + + TRACE( "iface %p, name %s, value %p.\n", iface, debugstr_hstring( name ), value ); + + if (FAILED(hr = IJsonObject_GetNamedValue( iface, name, &internal_value ))) + return hr; + + IJsonValue_Release( internal_value ); + IJsonValue_get_ValueType( internal_value, &value_type ); + if (value_type != JsonValueType_String) return E_ILLEGAL_METHOD_CALL; + + return IJsonValue_GetString( internal_value, value ); } static HRESULT WINAPI json_object_statics_GetNamedNumber( IJsonObject *iface, HSTRING name, DOUBLE *value ) { - FIXME( "iface %p, name %s, value %p stub!\n", iface, debugstr_hstring( name ), value ); - return E_NOTIMPL; + IJsonValue *internal_value; + JsonValueType value_type; + HRESULT hr; + + TRACE( "iface %p, name %s, value %p.\n", iface, debugstr_hstring( name ), value ); + + if (FAILED(hr = IJsonObject_GetNamedValue( iface, name, &internal_value ))) + return hr; + + IJsonValue_Release( internal_value ); + IJsonValue_get_ValueType( internal_value, &value_type ); + if (value_type != JsonValueType_Number) return E_ILLEGAL_METHOD_CALL; + + return IJsonValue_GetNumber( internal_value, value ); } static HRESULT WINAPI json_object_statics_GetNamedBoolean( IJsonObject *iface, HSTRING name, boolean *value ) { - FIXME( "iface %p, name %s, value %p stub!\n", iface, debugstr_hstring( name ), value ); - return E_NOTIMPL; + IJsonValue *internal_value; + JsonValueType value_type; + HRESULT hr; + + TRACE( "iface %p, name %s, value %p.\n", iface, debugstr_hstring( name ), value ); + + if (FAILED(hr = IJsonObject_GetNamedValue( iface, name, &internal_value ))) + return hr; + + IJsonValue_Release( internal_value ); + IJsonValue_get_ValueType( internal_value, &value_type ); + if (value_type != JsonValueType_Boolean) return E_ILLEGAL_METHOD_CALL; + + return IJsonValue_GetBoolean( internal_value, value ); } static const struct IJsonObjectVtbl json_object_statics_vtbl = @@ -220,7 +296,12 @@ static HRESULT WINAPI factory_GetTrustLevel( IActivationFactory *iface, TrustLev static HRESULT WINAPI factory_ActivateInstance( IActivationFactory *iface, IInspectable **instance ) { + const WCHAR *property_set_str = RuntimeClass_Windows_Foundation_Collections_PropertySet; + IPropertySet *property_set; + HSTRING property_set_class; struct json_object *impl; + HSTRING_HEADER header; + HRESULT hr; TRACE( "iface %p, instance %p.\n", iface, instance ); @@ -230,6 +311,22 @@ static HRESULT WINAPI factory_ActivateInstance( IActivationFactory *iface, IInsp impl->IJsonObject_iface.lpVtbl = &json_object_statics_vtbl; impl->ref = 1; + WindowsCreateStringReference( property_set_str, + wcslen( property_set_str ), &header, &property_set_class ); + if (FAILED(hr = RoActivateInstance( property_set_class, (IInspectable**)&property_set ))) + { + free( impl ); + return hr; + } + hr = IPropertySet_QueryInterface( property_set, + &IID_IMap_HSTRING_IInspectable, (void**)&impl->members ); + IPropertySet_Release( property_set ); + if (FAILED(hr)) + { + free( impl ); + return hr; + } + *instance = (IInspectable *)&impl->IJsonObject_iface; return S_OK; } diff --git a/dlls/windows.web/json_value.c b/dlls/windows.web/json_value.c index f1930beaad3..c712335af41 100644 --- a/dlls/windows.web/json_value.c +++ b/dlls/windows.web/json_value.c @@ -1,6 +1,7 @@ /* WinRT Windows.Data.Json.JsonValue Implementation * * Copyright (C) 2024 Mohamad Al-Jaf + * Copyright (C) 2026 Olivia Ryan * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -124,7 +125,8 @@ struct json_value HSTRING parsed_string; double parsed_number; boolean parsed_boolean; - HSTRING string_value; + IJsonArray *parsed_array; + IJsonObject *parsed_object; }; static inline struct json_value *impl_from_IJsonValue( IJsonValue *iface ) @@ -171,7 +173,8 @@ static ULONG WINAPI json_value_Release( IJsonValue *iface ) if (!ref) { WindowsDeleteString( impl->parsed_string ); - WindowsDeleteString( impl->string_value ); + if ( impl->parsed_array ) IJsonArray_Release( impl->parsed_array ); + if ( impl->parsed_object ) IJsonObject_Release( impl->parsed_object ); free( impl ); } return ref; @@ -255,24 +258,28 @@ static HRESULT WINAPI json_value_GetArray( IJsonValue *iface, IJsonArray **value { struct json_value *impl = impl_from_IJsonValue( iface ); - FIXME( "iface %p, value %p stub!\n", iface, value ); + TRACE( "iface %p, value %p\n", iface, value ); if (!value) return E_POINTER; if (impl->json_value_type != JsonValueType_Array) return E_ILLEGAL_METHOD_CALL; - return E_NOTIMPL; + IJsonArray_AddRef( impl->parsed_array ); + *value = impl->parsed_array; + return S_OK; } static HRESULT WINAPI json_value_GetObject( IJsonValue *iface, IJsonObject **value ) { struct json_value *impl = impl_from_IJsonValue( iface ); - FIXME( "iface %p, value %p stub!\n", iface, value ); + TRACE( "iface %p, value %p\n", iface, value ); if (!value) return E_POINTER; if (impl->json_value_type != JsonValueType_Object) return E_ILLEGAL_METHOD_CALL; - return E_NOTIMPL; + IJsonObject_AddRef( impl->parsed_object ); + *value = impl->parsed_object; + return S_OK; } static const struct IJsonValueVtbl json_value_vtbl = @@ -296,72 +303,293 @@ static const struct IJsonValueVtbl json_value_vtbl = DEFINE_IINSPECTABLE( json_value_statics, IJsonValueStatics, struct json_value_statics, IActivationFactory_iface ) -static HRESULT unescape_string( const WCHAR *src, HSTRING *output ) +static void trim_string( const WCHAR **input, UINT32 *len ) { - UINT32 len = wcslen( src ) - 1, n; - const WCHAR *end = src + len; - HSTRING_BUFFER buf; + static const WCHAR valid_whitespace[] = L" \t\n\r"; + UINT32 end = *len, start = 0; + + while (start < end && wcschr( valid_whitespace, (*input)[start] )) start++; + while (end > start && wcschr( valid_whitespace, (*input)[end - 1] )) end--; + + *len = end - start; + *input += start; +} + +static HRESULT parse_json_value( const WCHAR **json, UINT32 *len, struct json_value *impl ); + +static HRESULT parse_json_array( const WCHAR **json, UINT32 *len, struct json_value *impl ) +{ + struct json_value *child; + IJsonArray *array; HRESULT hr; + + TRACE( "json %s, impl %p", debugstr_wn( *json, *len ), impl ); + + if (FAILED(hr = IActivationFactory_ActivateInstance( + json_array_factory, (IInspectable**)&array ))) return hr; + + (*json)++; + (*len)--; + + trim_string( json, len ); + while (len && **json != ']') + { + if (!(child = calloc( 1, sizeof( *child ) ))) + { + IJsonArray_Release( array ); + return E_OUTOFMEMORY; + } + + child->IJsonValue_iface.lpVtbl = &json_value_vtbl; + child->ref = 1; + + if (FAILED(hr = parse_json_value( json, len, child ))) + { + IJsonValue_Release( &child->IJsonValue_iface ); + IJsonArray_Release( array ); + return hr; + } + + if (FAILED(hr = json_array_push( array, &child->IJsonValue_iface ))) + { + IJsonValue_Release( &child->IJsonValue_iface ); + IJsonArray_Release( array ); + return hr; + } + + trim_string( json, len ); + + if (**json == ',') + { + (*json)++; + (*len)--; + } + else if (**json != ']') + { + IJsonArray_Release( array ); + return WEB_E_INVALID_JSON_STRING; + } + + trim_string( json, len ); + } + + (*json)++; + (*len)--; + + impl->parsed_array = array; + impl->json_value_type = JsonValueType_Array; + return S_OK; +} + +static HRESULT parse_json_string( const WCHAR **json, UINT32 *len, HSTRING *output ) +{ + const WCHAR valid_hex_chars[] = L"abcdefABCDEF0123456789"; + const WCHAR valid_escape_chars[] = L"\"\\/bfnrtu"; + UINT32 string_len = 0; + HSTRING_BUFFER buf; + UINT32 offset = 0; + HRESULT hr = S_OK; WCHAR *dst; - for (len = n = 0; len + n < end - src; len++) { if (src[len + n] == '\\') n++; } - if (FAILED(hr = WindowsPreallocateStringBuffer( len, &dst, &buf ))) return hr; - while (src != end) { if (*src == '\\' && ++src == end) break; *dst++ = *src++; } + TRACE( "json %s, output %p", debugstr_wn( *json, *len ), output ); + + (*json)++; + (*len)--; + + /* validate string */ + + while (offset < *len) + { + if ((*json)[offset] < 32) return WEB_E_INVALID_JSON_STRING; + + if ((*json)[offset] == '\\') + { + if (*len - offset < 3 || + !wcschr( valid_escape_chars, (*json)[offset + 1]) || + ( (*json)[offset + 1] == 'u' && ( + !wcschr( valid_hex_chars, (*json)[offset + 2]) || + !wcschr( valid_hex_chars, (*json)[offset + 3]) || + !wcschr( valid_hex_chars, (*json)[offset + 4]) || + !wcschr( valid_hex_chars, (*json)[offset + 5]) ) )) + return WEB_E_INVALID_JSON_STRING; + + string_len++; + if ((*json)[offset + 1] == 'u') offset += 6; + else offset += 2; + } + else if ((*json)[offset] == '"') break; + else + { + string_len++; + offset++; + } + } + + if (offset == *len) return WEB_E_INVALID_JSON_STRING; + + /* create & escape string */ + + if (FAILED(hr = WindowsPreallocateStringBuffer( string_len, &dst, &buf ))) return hr; + + for (UINT32 i = 0; i < string_len; i++) + { + if (**json == '\\') + { + (*json)++; + *len -= 2; + if (**json == '"') { *dst++ = '"'; (*json)++; } + else if (**json == '\\') { *dst++ = '\\'; (*json)++; } + else if (**json == '/') { *dst++ = '/'; (*json)++; } + else if (**json == 'b') { *dst++ = '\b'; (*json)++; } + else if (**json == 'f') { *dst++ = '\f'; (*json)++; } + else if (**json == 'n') { *dst++ = '\n'; (*json)++; } + else if (**json == 'r') { *dst++ = '\r'; (*json)++; } + else if (**json == 't') { *dst++ = '\t'; (*json)++; } + else + { + (*json)++; + *len -= 4; + if (**json >= 65) *dst = ((*(*json)++ & 0x7) + 10) << 12; + else *dst = (*(*json)++ & 0xf) << 12; + + if (**json >= 65) *dst |= ((*(*json)++ & 0x7) + 10) << 8; + else *dst |= (*(*json)++ & 0xf) << 8; + + if (**json >= 65) *dst |= ((*(*json++) & 0x7) + 10) << 4; + else *dst |= (*(*json)++ & 0xf) << 4; + + if (**json >= 65) *dst++ |= (*(*json)++ & 0x7) + 10; + else *dst++ |= *(*json)++ & 0xf; + } + } + else + { + *dst++ = *(*json)++; + (*len)--; + } + } + + (*json)++; + (*len)--; return WindowsPromoteStringBuffer( buf, output ); } -static HRESULT trim_string( HSTRING input, HSTRING *output ) +static HRESULT parse_json_object( const WCHAR **json, UINT32 *len, struct json_value *impl ) { - static const WCHAR valid_whitespace[] = L" \t\n\r"; - UINT32 len, start = 0, end; - const WCHAR *json = WindowsGetStringRawBuffer( input, &len ); + struct json_value *child; + HSTRING name; + HRESULT hr; + + TRACE( "json %s, impl %p", debugstr_wn( *json, *len ), impl ); + + if (FAILED(hr = IActivationFactory_ActivateInstance( + json_object_factory, (IInspectable**)&impl->parsed_object ))) return hr; - end = len; - while (start < end && wcschr( valid_whitespace, json[start] )) start++; - while (end > start && wcschr( valid_whitespace, json[end - 1] )) end--; + (*json)++; + (*len)--; - return WindowsCreateString( json + start, end - start, output ); + trim_string( json, len ); + while (*len && **json != '}') + { + if (FAILED(hr = parse_json_string( json, len, &name ))) + { + IJsonObject_Release( impl->parsed_object ); + return hr; + } + + trim_string( json, len ); + if (!*len || **json != ':') + { + IJsonObject_Release( impl->parsed_object ); + WindowsDeleteString( name ); + return WEB_E_INVALID_JSON_STRING; + } + (*json)++; + (*len)--; + trim_string( json, len ); + + if (!(child = calloc( 1, sizeof( *child ) ))) + { + IJsonObject_Release( impl->parsed_object ); + WindowsDeleteString( name ); + return E_OUTOFMEMORY; + } + + child->IJsonValue_iface.lpVtbl = &json_value_vtbl; + child->ref = 1; + + if (FAILED(hr = parse_json_value( json, len, child ))) + { + IJsonValue_Release( &child->IJsonValue_iface ); + IJsonObject_Release( impl->parsed_object ); + WindowsDeleteString( name ); + return hr; + } + + hr = IJsonObject_SetNamedValue( impl->parsed_object, name, &child->IJsonValue_iface ); + IJsonValue_Release( &child->IJsonValue_iface ); + if (FAILED(hr)) + { + IJsonObject_Release( impl->parsed_object ); + WindowsDeleteString( name ); + return hr; + } + + trim_string( json, len ); + if (**json == ',') (*json)++; + else if (**json != '}') return WEB_E_INVALID_JSON_STRING; + trim_string( json, len ); + } + + if (!*len) return WEB_E_INVALID_JSON_STRING; + (*json)++; + (*len)--; + + impl->json_value_type = JsonValueType_Object; + return S_OK; } -static HRESULT parse_json_value( HSTRING input, struct json_value *impl ) +static HRESULT parse_json_value( const WCHAR **json, UINT32 *len, struct json_value *impl ) { - UINT32 len; - const WCHAR *json = WindowsGetStringRawBuffer( input, &len ); HRESULT hr = S_OK; - /* FIXME: Handle all JSON edge cases */ + TRACE( "json %s, impl %p\n", debugstr_wn( *json, *len ), impl ); - if (!len) return WEB_E_INVALID_JSON_STRING; + if (!*len) return WEB_E_INVALID_JSON_STRING; - if (len == 4 && !wcsncmp( L"null", json, 4 )) + if (*len >= 4 && !wcsncmp( L"null", *json, 4 )) { + *json += 4; + *len -= 4; impl->json_value_type = JsonValueType_Null; } - else if ((len == 4 && !wcsncmp( L"true", json, 4 )) || (len == 5 && !wcsncmp( L"false", json, 5 ))) + else if (*len >= 4 && !wcsncmp( L"true", *json, 4)) { - impl->parsed_boolean = len == 4; + *json += 4; + *len -= 4; + impl->parsed_boolean = true; impl->json_value_type = JsonValueType_Boolean; } - else if (json[0] == '\"' && json[len - 1] == '\"') + else if (*len >= 5 && !wcsncmp( L"false", *json, 5 )) { - json++; - len -= 2; - - if (len <= 2) return WEB_E_INVALID_JSON_STRING; - if (FAILED(hr = unescape_string( json, &impl->parsed_string ))) return hr; - + *json += 5; + *len -= 5; + impl->parsed_boolean = false; + impl->json_value_type = JsonValueType_Boolean; + } + else if (**json == '\"') + { + if (FAILED(hr = parse_json_string( json, len, &impl->parsed_string ))) return hr; impl->json_value_type = JsonValueType_String; } - else if (json[0] == '[' && json[len - 1] == ']') + else if (**json == '[') { - FIXME( "Array parsing not implemented!\n" ); - impl->json_value_type = JsonValueType_Array; + if (FAILED(hr = parse_json_array( json, len, impl ))) return hr; } - else if (json[0] == '{' && json[len - 1] == '}') + else if (**json == '{') { - FIXME( "Object parsing not implemented!\n" ); - impl->json_value_type = JsonValueType_Object; + if (FAILED(hr = parse_json_object( json, len, impl ))) return hr; } else { @@ -369,9 +597,12 @@ static HRESULT parse_json_value( HSTRING input, struct json_value *impl ) WCHAR *end; errno = 0; - result = wcstold( json, &end ); + result = wcstold( *json, &end ); - if (errno || errno == ERANGE || end != json + len) return WEB_E_INVALID_JSON_NUMBER; + *len -= end - *json; + *json = end; + + if (errno || errno == ERANGE) return WEB_E_INVALID_JSON_NUMBER; impl->parsed_number = result; impl->json_value_type = JsonValueType_Number; @@ -380,16 +611,17 @@ static HRESULT parse_json_value( HSTRING input, struct json_value *impl ) return hr; } -static HRESULT parse_json( HSTRING json, struct json_value *impl ) +static HRESULT parse_json( HSTRING string, struct json_value *impl ) { - HSTRING trimmed_json = NULL; - HRESULT hr = trim_string( json, &trimmed_json ); - - if (SUCCEEDED(hr) && WindowsIsStringEmpty( trimmed_json )) hr = WEB_E_INVALID_JSON_STRING; - if (SUCCEEDED(hr)) hr = parse_json_value( trimmed_json, impl ); + HRESULT hr; + UINT32 len; + const WCHAR *json = WindowsGetStringRawBuffer( string, &len ); - WindowsDeleteString( trimmed_json ); - return hr; + trim_string( &json, &len ); + if (!len) return WEB_E_INVALID_JSON_STRING; + if (FAILED(hr = parse_json_value( &json, &len, impl ))) return hr; + if (len) return WEB_E_INVALID_JSON_STRING; + return S_OK; } static HRESULT WINAPI json_value_statics_Parse( IJsonValueStatics *iface, HSTRING input, IJsonValue **value ) @@ -397,7 +629,7 @@ static HRESULT WINAPI json_value_statics_Parse( IJsonValueStatics *iface, HSTRIN struct json_value *impl; HRESULT hr; - FIXME( "iface %p, input %s, value %p semi-stub\n", iface, debugstr_hstring( input ), value ); + TRACE( "iface %p, input %s, value %p\n", iface, debugstr_hstring( input ), value ); if (!value) return E_POINTER; if (!input) return WEB_E_INVALID_JSON_STRING; @@ -424,14 +656,40 @@ static HRESULT WINAPI json_value_statics_TryParse( IJsonValueStatics *iface, HST static HRESULT WINAPI json_value_statics_CreateBooleanValue( IJsonValueStatics *iface, boolean input, IJsonValue **value ) { - FIXME( "iface %p, input %d, value %p stub!\n", iface, input, value ); - return E_NOTIMPL; + struct json_value *impl; + + TRACE( "iface %p, input %d, value %p\n", iface, input, value ); + + if (!value) return E_POINTER; + if (!(impl = calloc( 1, sizeof(*impl) ))) return E_OUTOFMEMORY; + + impl->IJsonValue_iface.lpVtbl = &json_value_vtbl; + impl->ref = 1; + impl->json_value_type = JsonValueType_Boolean; + impl->parsed_boolean = input != FALSE; + + *value = &impl->IJsonValue_iface; + TRACE( "created IJsonValue %p.\n", *value ); + return S_OK; } static HRESULT WINAPI json_value_statics_CreateNumberValue( IJsonValueStatics *iface, DOUBLE input, IJsonValue **value ) { - FIXME( "iface %p, input %f, value %p stub!\n", iface, input, value ); - return E_NOTIMPL; + struct json_value *impl; + + TRACE( "iface %p, input %f, value %p\n", iface, input, value ); + + if (!value) return E_POINTER; + if (!(impl = calloc( 1, sizeof(*impl) ))) return E_OUTOFMEMORY; + + impl->IJsonValue_iface.lpVtbl = &json_value_vtbl; + impl->ref = 1; + impl->json_value_type = JsonValueType_Number; + impl->parsed_number = input; + + *value = &impl->IJsonValue_iface; + TRACE( "created IJsonValue %p.\n", *value ); + return S_OK; } static HRESULT WINAPI json_value_statics_CreateStringValue( IJsonValueStatics *iface, HSTRING input, IJsonValue **value ) @@ -447,7 +705,7 @@ static HRESULT WINAPI json_value_statics_CreateStringValue( IJsonValueStatics *i impl->IJsonValue_iface.lpVtbl = &json_value_vtbl; impl->ref = 1; impl->json_value_type = JsonValueType_String; - if (FAILED(hr = WindowsDuplicateString( input, &impl->string_value ))) + if (FAILED(hr = WindowsDuplicateString( input, &impl->parsed_string ))) { free( impl ); return hr; diff --git a/dlls/windows.web/main.c b/dlls/windows.web/main.c index 60e95fdba0c..bec92a81775 100644 --- a/dlls/windows.web/main.c +++ b/dlls/windows.web/main.c @@ -38,6 +38,8 @@ HRESULT WINAPI DllGetActivationFactory( HSTRING classid, IActivationFactory **fa *factory = NULL; + if (!wcscmp( buffer, RuntimeClass_Windows_Data_Json_JsonArray )) + IActivationFactory_QueryInterface( json_array_factory, &IID_IActivationFactory, (void **)factory ); if (!wcscmp( buffer, RuntimeClass_Windows_Data_Json_JsonObject )) IActivationFactory_QueryInterface( json_object_factory, &IID_IActivationFactory, (void **)factory ); if (!wcscmp( buffer, RuntimeClass_Windows_Data_Json_JsonValue )) diff --git a/dlls/windows.web/private.h b/dlls/windows.web/private.h index d5f0e76d641..0cc2a351199 100644 --- a/dlls/windows.web/private.h +++ b/dlls/windows.web/private.h @@ -29,6 +29,7 @@ #include "winstring.h" #include "activation.h" +#include "roapi.h" #define WIDL_using_Windows_Foundation #define WIDL_using_Windows_Foundation_Collections @@ -36,9 +37,13 @@ #define WIDL_using_Windows_Data_Json #include "windows.data.json.h" +extern IActivationFactory *json_array_factory; extern IActivationFactory *json_object_factory; extern IActivationFactory *json_value_factory; +HRESULT json_array_push( IJsonArray *iface, IJsonValue *value ); +HRESULT json_array_pop( IJsonArray *iface, IJsonValue **value ); + #define DEFINE_IINSPECTABLE_( pfx, iface_type, impl_type, impl_from, iface_mem, expr ) \ static inline impl_type *impl_from( iface_type *iface ) \ { \ diff --git a/dlls/windows.web/tests/web.c b/dlls/windows.web/tests/web.c index b0f38edfb3b..0c1d822ed3c 100644 --- a/dlls/windows.web/tests/web.c +++ b/dlls/windows.web/tests/web.c @@ -45,6 +45,51 @@ static void check_interface_( unsigned int line, void *obj, const IID *iid ) IUnknown_Release( unk ); } +static void test_JsonArrayStatics(void) +{ + static const WCHAR *json_array_name = L"Windows.Data.Json.JsonArray"; + IActivationFactory *factory = (void *)0xdeadbeef; + IInspectable *inspectable = (void *)0xdeadbeef; + IJsonArray *json_array = (void *)0xdeadbeef; + HSTRING str = NULL; + HRESULT hr; + LONG ref; + + hr = WindowsCreateString( json_array_name, wcslen( json_array_name ), &str ); + ok( hr == S_OK, "got hr %#lx.\n", hr ); + hr = RoGetActivationFactory( str, &IID_IActivationFactory, (void **)&factory ); + WindowsDeleteString( str ); + ok( hr == S_OK || broken( hr == REGDB_E_CLASSNOTREG ), "got hr %#lx.\n", hr ); + if (hr == REGDB_E_CLASSNOTREG) + { + win_skip( "%s runtimeclass not registered, skipping tests.\n", wine_dbgstr_w( json_array_name ) ); + return; + } + + check_interface( factory, &IID_IUnknown ); + check_interface( factory, &IID_IInspectable ); + check_interface( factory, &IID_IAgileObject ); + + hr = IActivationFactory_QueryInterface( factory, &IID_IJsonArray, (void **)&json_array ); + ok( hr == E_NOINTERFACE, "got hr %#lx.\n", hr ); + + hr = WindowsCreateString( json_array_name, wcslen( json_array_name ), &str ); + ok( hr == S_OK, "got hr %#lx.\n", hr ); + hr = RoActivateInstance( str, &inspectable ); + ok( hr == S_OK, "got hr %#lx.\n", hr ); + WindowsDeleteString( str ); + + hr = IInspectable_QueryInterface( inspectable, &IID_IJsonArray, (void **)&json_array ); + ok( hr == S_OK, "got hr %#lx.\n", hr ); + + check_interface( inspectable, &IID_IAgileObject ); + + IJsonArray_Release( json_array ); + IInspectable_Release( inspectable ); + ref = IActivationFactory_Release( factory ); + ok( ref == 1, "got ref %ld.\n", ref ); +} + static void test_JsonObjectStatics(void) { static const WCHAR *json_object_name = L"Windows.Data.Json.JsonObject"; @@ -112,7 +157,6 @@ static void check_json_( unsigned int line, IJsonValueStatics *json_value_static if (expected_json_value_type == JsonValueType_Number) ok_(__FILE__, line)( hr == WEB_E_INVALID_JSON_NUMBER, "got hr %#lx.\n", hr ); else - todo_wine ok_(__FILE__, line)( hr == WEB_E_INVALID_JSON_STRING, "got hr %#lx.\n", hr ); WindowsDeleteString( str ); @@ -187,13 +231,11 @@ static void check_json_( unsigned int line, IJsonValueStatics *json_value_static break; case JsonValueType_Array: hr = IJsonValue_GetArray( json_value, &json_array ); - todo_wine ok_(__FILE__, line)( hr == S_OK, "got hr %#lx.\n", hr ); if (hr == S_OK) IJsonArray_Release( json_array ); break; case JsonValueType_Object: hr = IJsonValue_GetObject( json_value, &json_object ); - todo_wine ok_(__FILE__, line)( hr == S_OK, "got hr %#lx.\n", hr ); if (hr == S_OK) IJsonObject_Release( json_object ); break; @@ -289,7 +331,6 @@ static void test_JsonValueStatics(void) hr = IJsonValueStatics_Parse( json_value_statics, str, NULL ); ok( hr == E_POINTER, "got hr %#lx.\n", hr ); hr = IJsonValueStatics_Parse( json_value_statics, str, &json_value ); - todo_wine ok( hr == WEB_E_INVALID_JSON_STRING, "got hr %#lx.\n", hr ); WindowsDeleteString( str ); @@ -404,6 +445,7 @@ START_TEST(web) hr = RoInitialize( RO_INIT_MULTITHREADED ); ok( hr == S_OK, "RoInitialize failed, hr %#lx\n", hr ); + test_JsonArrayStatics(); test_JsonObjectStatics(); test_JsonValueStatics(); -- GitLab https://gitlab.winehq.org/wine/wine/-/merge_requests/10263