1 module javascriptcore.Value; 2 3 private import glib.Bytes; 4 private import glib.ConstructionException; 5 private import glib.PtrArray; 6 private import glib.Str; 7 private import gobject.ObjectG; 8 private import javascriptcore.Class; 9 private import javascriptcore.Context; 10 private import javascriptcore.c.functions; 11 public import javascriptcore.c.types; 12 13 14 /** 15 * JSCValue represents a reference to a value in a #JSCContext. The JSCValue 16 * protects the referenced value from being garbage collected. 17 */ 18 public class Value : ObjectG 19 { 20 /** the main Gtk struct */ 21 protected JSCValue* jSCValue; 22 23 /** Get the main Gtk struct */ 24 public JSCValue* getValueStruct(bool transferOwnership = false) 25 { 26 if (transferOwnership) 27 ownedRef = false; 28 return jSCValue; 29 } 30 31 /** the main Gtk struct as a void* */ 32 protected override void* getStruct() 33 { 34 return cast(void*)jSCValue; 35 } 36 37 /** 38 * Sets our main struct and passes it to the parent class. 39 */ 40 public this (JSCValue* jSCValue, bool ownedRef = false) 41 { 42 this.jSCValue = jSCValue; 43 super(cast(GObject*)jSCValue, ownedRef); 44 } 45 46 47 /** */ 48 public static GType getType() 49 { 50 return jsc_value_get_type(); 51 } 52 53 /** 54 * Create a new #JSCValue referencing an array with the items from @array. If @array 55 * is %NULL or empty a new empty array will be created. Elements of @array should be 56 * pointers to a #JSCValue. 57 * 58 * Params: 59 * context = a #JSCContext 60 * array = a #GPtrArray 61 * 62 * Returns: a #JSCValue. 63 * 64 * Throws: ConstructionException GTK+ fails to create the object. 65 */ 66 public this(Context context, PtrArray array) 67 { 68 auto __p = jsc_value_new_array_from_garray((context is null) ? null : context.getContextStruct(), (array is null) ? null : array.getPtrArrayStruct()); 69 70 if(__p is null) 71 { 72 throw new ConstructionException("null returned by new_array_from_garray"); 73 } 74 75 this(cast(JSCValue*) __p, true); 76 } 77 78 /** 79 * Create a new #JSCValue referencing an array of strings with the items from @strv. If @array 80 * is %NULL or empty a new empty array will be created. 81 * 82 * Params: 83 * context = a #JSCContext 84 * strv = a %NULL-terminated array of strings 85 * 86 * Returns: a #JSCValue. 87 * 88 * Throws: ConstructionException GTK+ fails to create the object. 89 */ 90 public this(Context context, string[] strv) 91 { 92 auto __p = jsc_value_new_array_from_strv((context is null) ? null : context.getContextStruct(), Str.toStringzArray(strv)); 93 94 if(__p is null) 95 { 96 throw new ConstructionException("null returned by new_array_from_strv"); 97 } 98 99 this(cast(JSCValue*) __p, true); 100 } 101 102 /** 103 * Create a new #JSCValue from @value 104 * 105 * Params: 106 * context = a #JSCContext 107 * value = a #gboolean 108 * 109 * Returns: a #JSCValue. 110 * 111 * Throws: ConstructionException GTK+ fails to create the object. 112 */ 113 public this(Context context, bool value) 114 { 115 auto __p = jsc_value_new_boolean((context is null) ? null : context.getContextStruct(), value); 116 117 if(__p is null) 118 { 119 throw new ConstructionException("null returned by new_boolean"); 120 } 121 122 this(cast(JSCValue*) __p, true); 123 } 124 125 /** 126 * Create a new #JSCValue referencing a new value created by parsing @json. 127 * 128 * Params: 129 * context = a #JSCContext 130 * json = the JSON string to be parsed 131 * 132 * Returns: a #JSCValue. 133 * 134 * Since: 2.28 135 * 136 * Throws: ConstructionException GTK+ fails to create the object. 137 */ 138 public static Value newJSON(Context context, string json) 139 { 140 return new Value(jsc_value_new_from_json((context is null) ? null : context.getContextStruct(), Str.toStringz(json)), true); 141 } 142 143 /** 144 * Create a function in @context. If @name is %NULL an anonymous function will be created. 145 * When the function is called by JavaScript or jsc_value_function_call(), @callback is called 146 * receiving an #GPtrArray of #JSCValue<!-- -->s with the arguments and then @user_data as last parameter. 147 * When the function is cleared in @context, @destroy_notify is called with @user_data as parameter. 148 * 149 * Note that the value returned by @callback must be fully transferred. In case of boxed types, you could use 150 * %G_TYPE_POINTER instead of the actual boxed #GType to ensure that the instance owned by #JSCClass is used. 151 * If you really want to return a new copy of the boxed type, use #JSC_TYPE_VALUE and return a #JSCValue created 152 * with jsc_value_new_object() that receives the copy as instance parameter. 153 * 154 * Params: 155 * context = a #JSCContext 156 * name = the function name or %NULL 157 * callback = a #GCallback. 158 * userData = user data to pass to @callback. 159 * destroyNotify = destroy notifier for @user_data 160 * returnType = the #GType of the function return value, or %G_TYPE_NONE if the function is void. 161 * 162 * Returns: a #JSCValue. 163 * 164 * Throws: ConstructionException GTK+ fails to create the object. 165 */ 166 public this(Context context, string name, GCallback callback, void* userData, GDestroyNotify destroyNotify, GType returnType) 167 { 168 auto __p = jsc_value_new_function_variadic((context is null) ? null : context.getContextStruct(), Str.toStringz(name), callback, userData, destroyNotify, returnType); 169 170 if(__p is null) 171 { 172 throw new ConstructionException("null returned by new_function_variadic"); 173 } 174 175 this(cast(JSCValue*) __p, true); 176 } 177 178 /** 179 * Create a function in @context. If @name is %NULL an anonymous function will be created. 180 * When the function is called by JavaScript or jsc_value_function_call(), @callback is called 181 * receiving the function parameters and then @user_data as last parameter. When the function is 182 * cleared in @context, @destroy_notify is called with @user_data as parameter. 183 * 184 * Note that the value returned by @callback must be fully transferred. In case of boxed types, you could use 185 * %G_TYPE_POINTER instead of the actual boxed #GType to ensure that the instance owned by #JSCClass is used. 186 * If you really want to return a new copy of the boxed type, use #JSC_TYPE_VALUE and return a #JSCValue created 187 * with jsc_value_new_object() that receives the copy as instance parameter. 188 * 189 * Params: 190 * context = a #JSCContext 191 * name = the function name or %NULL 192 * callback = a #GCallback. 193 * userData = user data to pass to @callback. 194 * destroyNotify = destroy notifier for @user_data 195 * returnType = the #GType of the function return value, or %G_TYPE_NONE if the function is void. 196 * parameterTypes = a list of #GType<!-- -->s, one for each parameter, or %NULL 197 * 198 * Returns: a #JSCValue. 199 * 200 * Throws: ConstructionException GTK+ fails to create the object. 201 */ 202 public this(Context context, string name, GCallback callback, void* userData, GDestroyNotify destroyNotify, GType returnType, GType[] parameterTypes) 203 { 204 auto __p = jsc_value_new_functionv((context is null) ? null : context.getContextStruct(), Str.toStringz(name), callback, userData, destroyNotify, returnType, cast(uint)parameterTypes.length, parameterTypes.ptr); 205 206 if(__p is null) 207 { 208 throw new ConstructionException("null returned by new_functionv"); 209 } 210 211 this(cast(JSCValue*) __p, true); 212 } 213 214 /** 215 * Create a new #JSCValue referencing <function>null</function> in @context. 216 * 217 * Params: 218 * context = a #JSCContext 219 * 220 * Returns: a #JSCValue. 221 * 222 * Throws: ConstructionException GTK+ fails to create the object. 223 */ 224 public this(Context context) 225 { 226 auto __p = jsc_value_new_null((context is null) ? null : context.getContextStruct()); 227 228 if(__p is null) 229 { 230 throw new ConstructionException("null returned by new_null"); 231 } 232 233 this(cast(JSCValue*) __p, true); 234 } 235 236 /** 237 * Create a new #JSCValue from @number. 238 * 239 * Params: 240 * context = a #JSCContext 241 * number = a number 242 * 243 * Returns: a #JSCValue. 244 * 245 * Throws: ConstructionException GTK+ fails to create the object. 246 */ 247 public this(Context context, double number) 248 { 249 auto __p = jsc_value_new_number((context is null) ? null : context.getContextStruct(), number); 250 251 if(__p is null) 252 { 253 throw new ConstructionException("null returned by new_number"); 254 } 255 256 this(cast(JSCValue*) __p, true); 257 } 258 259 /** 260 * Create a new #JSCValue from @instance. If @instance is %NULL a new empty object is created. 261 * When @instance is provided, @jsc_class must be provided too. @jsc_class takes ownership of 262 * @instance that will be freed by the #GDestroyNotify passed to jsc_context_register_class(). 263 * 264 * Params: 265 * context = a #JSCContext 266 * instance_ = an object instance or %NULL 267 * jscClass = the #JSCClass of @instance 268 * 269 * Returns: a #JSCValue. 270 * 271 * Throws: ConstructionException GTK+ fails to create the object. 272 */ 273 public this(Context context, void* instance_, Class jscClass) 274 { 275 auto __p = jsc_value_new_object((context is null) ? null : context.getContextStruct(), instance_, (jscClass is null) ? null : jscClass.getClassStruct()); 276 277 if(__p is null) 278 { 279 throw new ConstructionException("null returned by new_object"); 280 } 281 282 this(cast(JSCValue*) __p, true); 283 } 284 285 /** 286 * Create a new #JSCValue from @string. If you need to create a #JSCValue from a 287 * string containing null characters, use jsc_value_new_string_from_bytes() instead. 288 * 289 * Params: 290 * context = a #JSCContext 291 * string_ = a null-terminated string 292 * 293 * Returns: a #JSCValue. 294 * 295 * Throws: ConstructionException GTK+ fails to create the object. 296 */ 297 public this(Context context, string string_) 298 { 299 auto __p = jsc_value_new_string((context is null) ? null : context.getContextStruct(), Str.toStringz(string_)); 300 301 if(__p is null) 302 { 303 throw new ConstructionException("null returned by new_string"); 304 } 305 306 this(cast(JSCValue*) __p, true); 307 } 308 309 /** 310 * Create a new #JSCValue from @bytes. 311 * 312 * Params: 313 * context = a #JSCContext 314 * bytes = a #GBytes 315 * 316 * Returns: a #JSCValue. 317 * 318 * Throws: ConstructionException GTK+ fails to create the object. 319 */ 320 public this(Context context, Bytes bytes) 321 { 322 auto __p = jsc_value_new_string_from_bytes((context is null) ? null : context.getContextStruct(), (bytes is null) ? null : bytes.getBytesStruct()); 323 324 if(__p is null) 325 { 326 throw new ConstructionException("null returned by new_string_from_bytes"); 327 } 328 329 this(cast(JSCValue*) __p, true); 330 } 331 332 /** 333 * Create a new #JSCValue referencing <function>undefined</function> in @context. 334 * 335 * Params: 336 * context = a #JSCContext 337 * 338 * Returns: a #JSCValue. 339 * 340 * Throws: ConstructionException GTK+ fails to create the object. 341 */ 342 public static Value newUndefined(Context context) 343 { 344 return new Value(jsc_value_new_undefined((context is null) ? null : context.getContextStruct()), true); 345 } 346 347 /** 348 * Invoke <function>new</function> with constructor referenced by @value. If @n_parameters 349 * is 0 no parameters will be passed to the constructor. 350 * 351 * Params: 352 * parameters = the #JSCValue<!-- -->s to pass as parameters to the constructor, or %NULL 353 * 354 * Returns: a #JSCValue referencing the newly created object instance. 355 */ 356 public Value constructorCallv(Value[] parameters) 357 { 358 JSCValue*[] parametersArray = new JSCValue*[parameters.length]; 359 for ( int i = 0; i < parameters.length; i++ ) 360 { 361 parametersArray[i] = parameters[i].getValueStruct(); 362 } 363 364 auto __p = jsc_value_constructor_callv(jSCValue, cast(uint)parameters.length, parametersArray.ptr); 365 366 if(__p is null) 367 { 368 return null; 369 } 370 371 return ObjectG.getDObject!(Value)(cast(JSCValue*) __p, true); 372 } 373 374 /** 375 * Call function referenced by @value, passing the given @parameters. If @n_parameters 376 * is 0 no parameters will be passed to the function. 377 * 378 * This function always returns a #JSCValue, in case of void functions a #JSCValue referencing 379 * <function>undefined</function> is returned 380 * 381 * Params: 382 * parameters = the #JSCValue<!-- -->s to pass as parameters to the function, or %NULL 383 * 384 * Returns: a #JSCValue with the return value of the function. 385 */ 386 public Value functionCallv(Value[] parameters) 387 { 388 JSCValue*[] parametersArray = new JSCValue*[parameters.length]; 389 for ( int i = 0; i < parameters.length; i++ ) 390 { 391 parametersArray[i] = parameters[i].getValueStruct(); 392 } 393 394 auto __p = jsc_value_function_callv(jSCValue, cast(uint)parameters.length, parametersArray.ptr); 395 396 if(__p is null) 397 { 398 return null; 399 } 400 401 return ObjectG.getDObject!(Value)(cast(JSCValue*) __p, true); 402 } 403 404 /** 405 * Get the #JSCContext in which @value was created. 406 * 407 * Returns: the #JSCValue context. 408 */ 409 public Context getContext() 410 { 411 auto __p = jsc_value_get_context(jSCValue); 412 413 if(__p is null) 414 { 415 return null; 416 } 417 418 return ObjectG.getDObject!(Context)(cast(JSCContext*) __p); 419 } 420 421 /** 422 * Get whether the value referenced by @value is an array. 423 * 424 * Returns: whether the value is an array. 425 */ 426 public bool isArray() 427 { 428 return jsc_value_is_array(jSCValue) != 0; 429 } 430 431 /** 432 * Get whether the value referenced by @value is a boolean. 433 * 434 * Returns: whether the value is a boolean. 435 */ 436 public bool isBoolean() 437 { 438 return jsc_value_is_boolean(jSCValue) != 0; 439 } 440 441 /** 442 * Get whether the value referenced by @value is a constructor. 443 * 444 * Returns: whether the value is a constructor. 445 */ 446 public bool isConstructor() 447 { 448 return jsc_value_is_constructor(jSCValue) != 0; 449 } 450 451 /** 452 * Get whether the value referenced by @value is a function 453 * 454 * Returns: whether the value is a function. 455 */ 456 public bool isFunction() 457 { 458 return jsc_value_is_function(jSCValue) != 0; 459 } 460 461 /** 462 * Get whether the value referenced by @value is <function>null</function>. 463 * 464 * Returns: whether the value is null. 465 */ 466 public bool isNull() 467 { 468 return jsc_value_is_null(jSCValue) != 0; 469 } 470 471 /** 472 * Get whether the value referenced by @value is a number. 473 * 474 * Returns: whether the value is a number. 475 */ 476 public bool isNumber() 477 { 478 return jsc_value_is_number(jSCValue) != 0; 479 } 480 481 /** 482 * Get whether the value referenced by @value is an object. 483 * 484 * Returns: whether the value is an object. 485 */ 486 public bool isObject() 487 { 488 return jsc_value_is_object(jSCValue) != 0; 489 } 490 491 /** 492 * Get whether the value referenced by @value is a string 493 * 494 * Returns: whether the value is a string 495 */ 496 public bool isString() 497 { 498 return jsc_value_is_string(jSCValue) != 0; 499 } 500 501 /** 502 * Get whether the value referenced by @value is <function>undefined</function>. 503 * 504 * Returns: whether the value is undefined. 505 */ 506 public bool isUndefined() 507 { 508 return jsc_value_is_undefined(jSCValue) != 0; 509 } 510 511 /** 512 * Define or modify a property with @property_name in object referenced by @value. When the 513 * property value needs to be getted or set, @getter and @setter callbacks will be called. 514 * When the property is cleared in the #JSCClass context, @destroy_notify is called with 515 * @user_data as parameter. This is equivalent to JavaScript <function>Object.defineProperty()</function> 516 * when used with an accessor descriptor. 517 * 518 * Note that the value returned by @getter must be fully transferred. In case of boxed types, you could use 519 * %G_TYPE_POINTER instead of the actual boxed #GType to ensure that the instance owned by #JSCClass is used. 520 * If you really want to return a new copy of the boxed type, use #JSC_TYPE_VALUE and return a #JSCValue created 521 * with jsc_value_new_object() that receives the copy as instance parameter. 522 * 523 * Params: 524 * propertyName = the name of the property to define 525 * flags = #JSCValuePropertyFlags 526 * propertyType = the #GType of the property 527 * getter = a #GCallback to be called to get the property value 528 * setter = a #GCallback to be called to set the property value 529 * userData = user data to pass to @getter and @setter 530 * destroyNotify = destroy notifier for @user_data 531 */ 532 public void objectDefinePropertyAccessor(string propertyName, JSCValuePropertyFlags flags, GType propertyType, GCallback getter, GCallback setter, void* userData, GDestroyNotify destroyNotify) 533 { 534 jsc_value_object_define_property_accessor(jSCValue, Str.toStringz(propertyName), flags, propertyType, getter, setter, userData, destroyNotify); 535 } 536 537 /** 538 * Define or modify a property with @property_name in object referenced by @value. This is equivalent to 539 * JavaScript <function>Object.defineProperty()</function> when used with a data descriptor. 540 * 541 * Params: 542 * propertyName = the name of the property to define 543 * flags = #JSCValuePropertyFlags 544 * propertyValue = the default property value 545 */ 546 public void objectDefinePropertyData(string propertyName, JSCValuePropertyFlags flags, Value propertyValue) 547 { 548 jsc_value_object_define_property_data(jSCValue, Str.toStringz(propertyName), flags, (propertyValue is null) ? null : propertyValue.getValueStruct()); 549 } 550 551 /** 552 * Try to delete property with @name from @value. This function will return %FALSE if 553 * the property was defined without %JSC_VALUE_PROPERTY_CONFIGURABLE flag. 554 * 555 * Params: 556 * name = the property name 557 * 558 * Returns: %TRUE if the property was deleted, or %FALSE otherwise. 559 */ 560 public bool objectDeleteProperty(string name) 561 { 562 return jsc_value_object_delete_property(jSCValue, Str.toStringz(name)) != 0; 563 } 564 565 /** 566 * Get the list of property names of @value. Only properties defined with %JSC_VALUE_PROPERTY_ENUMERABLE 567 * flag will be collected. 568 * 569 * Returns: a %NULL-terminated array of strings containing the 570 * property names, or %NULL if @value doesn't have enumerable properties. Use g_strfreev() to free. 571 */ 572 public string[] objectEnumerateProperties() 573 { 574 auto retStr = jsc_value_object_enumerate_properties(jSCValue); 575 576 scope(exit) Str.freeStringArray(retStr); 577 return Str.toStringArray(retStr); 578 } 579 580 /** 581 * Get property with @name from @value. 582 * 583 * Params: 584 * name = the property name 585 * 586 * Returns: the property #JSCValue. 587 */ 588 public Value objectGetProperty(string name) 589 { 590 auto __p = jsc_value_object_get_property(jSCValue, Str.toStringz(name)); 591 592 if(__p is null) 593 { 594 return null; 595 } 596 597 return ObjectG.getDObject!(Value)(cast(JSCValue*) __p, true); 598 } 599 600 /** 601 * Get property at @index from @value. 602 * 603 * Params: 604 * index = the property index 605 * 606 * Returns: the property #JSCValue. 607 */ 608 public Value objectGetPropertyAtIndex(uint index) 609 { 610 auto __p = jsc_value_object_get_property_at_index(jSCValue, index); 611 612 if(__p is null) 613 { 614 return null; 615 } 616 617 return ObjectG.getDObject!(Value)(cast(JSCValue*) __p, true); 618 } 619 620 /** 621 * Get whether @value has property with @name. 622 * 623 * Params: 624 * name = the property name 625 * 626 * Returns: %TRUE if @value has a property with @name, or %FALSE otherwise 627 */ 628 public bool objectHasProperty(string name) 629 { 630 return jsc_value_object_has_property(jSCValue, Str.toStringz(name)) != 0; 631 } 632 633 /** 634 * Invoke method with @name on object referenced by @value, passing the given @parameters. If 635 * @n_parameters is 0 no parameters will be passed to the method. 636 * The object instance will be handled automatically even when the method is a custom one 637 * registered with jsc_class_add_method(), so it should never be passed explicitly as parameter 638 * of this function. 639 * 640 * This function always returns a #JSCValue, in case of void methods a #JSCValue referencing 641 * <function>undefined</function> is returned. 642 * 643 * Params: 644 * name = the method name 645 * parameters = the #JSCValue<!-- -->s to pass as parameters to the method, or %NULL 646 * 647 * Returns: a #JSCValue with the return value of the method. 648 */ 649 public Value objectInvokeMethodv(string name, Value[] parameters) 650 { 651 JSCValue*[] parametersArray = new JSCValue*[parameters.length]; 652 for ( int i = 0; i < parameters.length; i++ ) 653 { 654 parametersArray[i] = parameters[i].getValueStruct(); 655 } 656 657 auto __p = jsc_value_object_invoke_methodv(jSCValue, Str.toStringz(name), cast(uint)parameters.length, parametersArray.ptr); 658 659 if(__p is null) 660 { 661 return null; 662 } 663 664 return ObjectG.getDObject!(Value)(cast(JSCValue*) __p, true); 665 } 666 667 /** 668 * Get whether the value referenced by @value is an instance of class @name. 669 * 670 * Params: 671 * name = a class name 672 * 673 * Returns: whether the value is an object instance of class @name. 674 */ 675 public bool objectIsInstanceOf(string name) 676 { 677 return jsc_value_object_is_instance_of(jSCValue, Str.toStringz(name)) != 0; 678 } 679 680 /** 681 * Set @property with @name on @value. 682 * 683 * Params: 684 * name = the property name 685 * property = the #JSCValue to set 686 */ 687 public void objectSetProperty(string name, Value property) 688 { 689 jsc_value_object_set_property(jSCValue, Str.toStringz(name), (property is null) ? null : property.getValueStruct()); 690 } 691 692 /** 693 * Set @property at @index on @value. 694 * 695 * Params: 696 * index = the property index 697 * property = the #JSCValue to set 698 */ 699 public void objectSetPropertyAtIndex(uint index, Value property) 700 { 701 jsc_value_object_set_property_at_index(jSCValue, index, (property is null) ? null : property.getValueStruct()); 702 } 703 704 /** 705 * Convert @value to a boolean. 706 * 707 * Returns: a #gboolean result of the conversion. 708 */ 709 public bool toBoolean() 710 { 711 return jsc_value_to_boolean(jSCValue) != 0; 712 } 713 714 /** 715 * Convert @value to a double. 716 * 717 * Returns: a #gdouble result of the conversion. 718 */ 719 public double toDouble() 720 { 721 return jsc_value_to_double(jSCValue); 722 } 723 724 /** 725 * Convert @value to a #gint32. 726 * 727 * Returns: a #gint32 result of the conversion. 728 */ 729 public int toInt32() 730 { 731 return jsc_value_to_int32(jSCValue); 732 } 733 734 /** 735 * Create a JSON string of @value serialization. If @indent is 0, the resulting JSON will 736 * not contain newlines. The size of the indent is clamped to 10 spaces. 737 * 738 * Params: 739 * indent = The number of spaces to indent when nesting. 740 * 741 * Returns: a null-terminated JSON string with serialization of @value 742 * 743 * Since: 2.28 744 */ 745 public string toJson(uint indent) 746 { 747 auto retStr = jsc_value_to_json(jSCValue, indent); 748 749 scope(exit) Str.freeString(retStr); 750 return Str.toString(retStr); 751 } 752 753 /** 754 * Convert @value to a string. Use jsc_value_to_string_as_bytes() instead, if you need to 755 * handle strings containing null characters. 756 * 757 * Returns: a null-terminated string result of the conversion. 758 */ 759 public override string toString() 760 { 761 auto retStr = jsc_value_to_string(jSCValue); 762 763 scope(exit) Str.freeString(retStr); 764 return Str.toString(retStr); 765 } 766 767 /** 768 * Convert @value to a string and return the results as #GBytes. This is needed 769 * to handle strings with null characters. 770 * 771 * Returns: a #GBytes with the result of the conversion. 772 */ 773 public Bytes toStringAsBytes() 774 { 775 auto __p = jsc_value_to_string_as_bytes(jSCValue); 776 777 if(__p is null) 778 { 779 return null; 780 } 781 782 return new Bytes(cast(GBytes*) __p, true); 783 } 784 }