Browse Source

QByteArray to/from float/double added to host environment

Neal Wilson 9 years ago
parent
commit
b679ed4fa0
1 changed files with 80 additions and 2 deletions
  1. 80
    2
      src/typica.w

+ 80
- 2
src/typica.w View File

@@ -2245,6 +2245,8 @@ QScriptValue QByteArray_remove(QScriptContext *context, QScriptEngine *engine);
2245 2245
 QScriptValue QByteArray_toInt8(QScriptContext *context, QScriptEngine *engine);
2246 2246
 QScriptValue QByteArray_toInt16(QScriptContext *context, QScriptEngine *engine);
2247 2247
 QScriptValue QByteArray_toInt32(QScriptContext *context, QScriptEngine *engine);
2248
+QScriptValue QByteArray_toFloat(QScriptContext *context, QScriptEngine *engine);
2249
+QScriptValue QByteArray_toDouble(QScriptContext *context, QScriptEngine *engine);
2248 2250
 
2249 2251
 @ First, we provide some functionns for moving array data across the
2250 2252
 language barrier.
@@ -2300,6 +2302,8 @@ void setQByteArrayProperties(QScriptValue value, QScriptEngine *engine)
2300 2302
 	value.setProperty("toInt8", engine->newFunction(QByteArray_toInt8));
2301 2303
 	value.setProperty("toInt16", engine->newFunction(QByteArray_toInt16));
2302 2304
 	value.setProperty("toInt32", engine->newFunction(QByteArray_toInt32));
2305
+	value.setProperty("toFloat", engine->newFunction(QByteArray_toFloat));
2306
+	value.setProperty("toDouble", engine->newFunction(QByteArray_toDouble));
2303 2307
 }
2304 2308
 
2305 2309
 @ Perhaps the easiest way to deal with fixed byte strings for serial
@@ -2459,6 +2463,39 @@ QScriptValue QByteArray_toInt32(QScriptContext *context, QScriptEngine *)
2459 2463
 	return QScriptValue(value);
2460 2464
 }
2461 2465
 
2466
+@ Similar methods are provided for converting bytes to a |float| or |double|.
2467
+Note that the return value from |toFloat| will, in the host environment, be
2468
+represented as a |double|.
2469
+
2470
+@<Functions for scripting@>=
2471
+QScriptValue QByteArray_toFloat(QScriptContext *context, QScriptEngine *)
2472
+{
2473
+	QByteArray self = getself<QByteArray>(context);
2474
+	float value = 0.0;
2475
+	char *bytes = (char *)&value;
2476
+	bytes[0] = self[0];
2477
+	bytes[1] = self[1];
2478
+	bytes[2] = self[2];
2479
+	bytes[3] = self[3];
2480
+	return QScriptValue(value);
2481
+}
2482
+
2483
+QScriptValue QByteArray_toDouble(QScriptContext *context, QScriptEngine *)
2484
+{
2485
+	QByteArray self = getself<QByteArray>(context);
2486
+	double value = 0.0;
2487
+	char *bytes = (char *)&value;
2488
+	bytes[0] = self[0];
2489
+	bytes[1] = self[1];
2490
+	bytes[2] = self[2];
2491
+	bytes[3] = self[3];
2492
+	bytes[4] = self[4];
2493
+	bytes[5] = self[5];
2494
+	bytes[6] = self[6];
2495
+	bytes[7] = self[7];
2496
+	return QScriptValue(value);
2497
+}
2498
+
2462 2499
 @ Some protocols require manipulating larger than 8 bit numbers as a sequence
2463 2500
 of bytes. To facilitate this, methods are provided to construct a |QByteArray|
2464 2501
 from different sized numbers. 8 bit numbers are provided for uniformity.
@@ -2467,6 +2504,8 @@ from different sized numbers. 8 bit numbers are provided for uniformity.
2467 2504
 QScriptValue bytesFromInt8(QScriptContext *context, QScriptEngine *engine);
2468 2505
 QScriptValue bytesFromInt16(QScriptContext *context, QScriptEngine *engine);
2469 2506
 QScriptValue bytesFromInt32(QScriptContext *context, QScriptEngine *engine);
2507
+QScriptValue bytesFromFloat(QScriptContext *context, QScriptEngine *engine);
2508
+QScriptValue bytesFromDouble(QScriptContext *context, QScriptEngine *engine);
2470 2509
 
2471 2510
 @ These are globally available.
2472 2511
 
@@ -2474,9 +2513,14 @@ QScriptValue bytesFromInt32(QScriptContext *context, QScriptEngine *engine);
2474 2513
 engine->globalObject().setProperty("bytesFromInt8", engine->newFunction(bytesFromInt8));
2475 2514
 engine->globalObject().setProperty("bytesFromInt16", engine->newFunction(bytesFromInt16));
2476 2515
 engine->globalObject().setProperty("bytesFromInt32", engine->newFunction(bytesFromInt32));
2516
+engine->globalObject().setProperty("bytesFromFloat", engine->newFunction(bytesFromFloat));
2517
+engine->globalObject().setProperty("bytesFromDouble", engine->newFunction(bytesFromDouble));
2477 2518
 
2478
-@ The methods all work by casting the appropriate integer type to a |char *|
2479
-and copying the bytes to a new |QByteArray|.
2519
+@ The methods all work by casting the appropriate numeric type to a |char *|
2520
+and copying the bytes to a new |QByteArray|. Note that the ECMA-262 standard
2521
+only has one type of number and this is an IEEE 754 binary64 double precision
2522
+floating point number. Functions other than |bytesFromDouble| will be cast
2523
+from |double|.
2480 2524
 
2481 2525
 @<Functions for scripting@>=
2482 2526
 QScriptValue bytesFromInt8(QScriptContext *context, QScriptEngine *engine)
@@ -2519,6 +2563,40 @@ QScriptValue bytesFromInt32(QScriptContext *context, QScriptEngine *engine)
2519 2563
 	return v;
2520 2564
 }
2521 2565
 
2566
+QScriptValue bytesFromFloat(QScriptContext *context, QScriptEngine *engine)
2567
+{
2568
+	float value = (float)(argument<double>(0, context));
2569
+	char *bytes = (char *)&value;
2570
+	QByteArray retval;
2571
+	retval.resize(4);
2572
+	retval[0] = bytes[0];
2573
+	retval[1] = bytes[1];
2574
+	retval[2] = bytes[2];
2575
+	retval[3] = bytes[3];
2576
+	QScriptValue v = engine->toScriptValue<QByteArray>(retval);
2577
+	setQByteArrayProperties(v, engine);
2578
+	return v;
2579
+}
2580
+
2581
+QScriptValue bytesFromDouble(QScriptContext *context, QScriptEngine *engine)
2582
+{
2583
+	double value = (double)(argument<double>(0, context));
2584
+	char *bytes = (char *)&value;
2585
+	QByteArray retval;
2586
+	retval.resize(8);
2587
+	retval[0] = bytes[0];
2588
+	retval[1] = bytes[1];
2589
+	retval[2] = bytes[2];
2590
+	retval[3] = bytes[3];
2591
+	retval[4] = bytes[4];
2592
+	retval[5] = bytes[5];
2593
+	retval[6] = bytes[6];
2594
+	retval[7] = bytes[7];
2595
+	QScriptValue v = engine->toScriptValue<QByteArray>(retval);
2596
+	setQByteArrayProperties(v, engine);
2597
+	return v;
2598
+}
2599
+
2522 2600
 @* Scripting QBuffer.
2523 2601
 
2524 2602
 \noindent Sometimes it is desirable to load a roast profile from a file. At

Loading…
Cancel
Save